开发者

C++: Trouble with parsing XML file (MXML library)

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.pu开发者_如何学Csh_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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜