views:

21

answers:

1

I am having an issue with the following code.

It uses the Mini-XML library to extract elements from an xml file.

It compiles and runs fine, except that it is unable to get the last "radius element," returning NULL instead (it crashes if I don't check to see if subnode is not NULL).

//Start loading <ssys> elements
mxml_node_t *node; //temporary node to save
mxml_node_t *subnode; //temporary subnode to save
mxml_node_t *subnode2; //another temporary subnode to save
const char* name_tmp; //Temporary string for names of ssys
//Load first ssys
node = mxmlFindElement(Systems_elem, tree, "ssys", NULL, NULL, MXML_DESCEND);
cout << "Node 1 pointer: " << node<<endl<<endl;
//Start loading the rest of the ssys elements (but fail if first element is NULL)
int i = 1;
do {
    //Load node into vector of pointers
    ssys_elem.push_back(node);
    //Get name attribute
    name_tmp = mxmlElementGetAttr(node, "name");
    ssys_name.push_back(name_tmp);
    //load next ssys
    node = mxmlFindElement(node, tree, "ssys", NULL, NULL, MXML_NO_DESCEND);
    cout << "Node: " << node<<endl<<endl;
    cout<<"Name: " <<name_tmp<<endl<<endl;
    //Descend to radius element
    subnode = mxmlFindElement(node, tree, "radius", NULL, NULL, MXML_DESCEND);
    //
    if(subnode != NULL){
        cout <<subnode->child->value.text.string<<endl<<endl; <--Trouble Here
    }
   i++;
}
while (node != NULL);

}

Here is a piece of the XML file in question:

 <ssys name="Zylex">
  <general>
   <radius>3500.000000</radius> <-- I am trying to get "3500.000000"
   <stars>400</stars>
   <asteroids>0</asteroids>
   <interference>300.000000</interference>
   <nebula volatility="0.000000">250.000000</nebula>
  </general>
  <pos>
   <x>438.000000</x>
   <y>-34.000000</y>
  </pos>
  <assets/>
  <jumps>
   <jump target="Arcanis">
    <pos x="-3418.937501" y="-748.910119"/>
    <radius>200.000000</radius>
    <flags>
     <autopos/>
    </flags>
   </jump>
   <jump target="Doeston">
    <pos x="2991.156265" y="1817.411401"/>
    <radius>200.000000</radius>
    <flags>
     <autopos/>
    </flags>
   </jump>
  </jumps>
 </ssys>
</Systems>

What's the problem with this code?

+1  A: 

Well I'm guessing you're talking about Mini-XML, which I honestly have no experience with. What I do see is that you're iterator is one step ahead of your name. I think you want something more like:

node = mxmlFindElement(Systems_elem, tree, "ssys", NULL, NULL, MXML_DESCEND);
while (node) {
    ssys_elem.push_back(node);
    name_tmp = mxmlElementGetAttr(node, "name");
    ssys_name.push_back(name_tmp);

    cout << "Node: " << node << endl << endl;
    if (name_tmp) {
        cout << "Name: " << name_tmp << endl << endl;
    }

    // Descend to radius element
    subnode = mxmlFindElement(node, tree, "radius", NULL, NULL, MXML_DESCEND);
    if (subnode != NULL){
        cout << subnode->child->value.text.string << endl << endl; <--Trouble Here
    }

    // iterate at the end!
    node = mxmlFindElement(node, tree, "ssys", NULL, NULL, MXML_NO_DESCEND);
}

And in general never trust that you have well-formed XML. Always check for NULL even when you think there should be a child. Hope that helps.

Eric Rahm
D'oh. Yeah, that's it, I incremented the node before loading. Wow, dumb mistake.
Biosci3c
Yeah, I'm using the Mini-XML library. I have edited the question to reflect this.
Biosci3c
Thanks for your help
Biosci3c