C++:解析 XML 文件时出现问题(MXML 库)

发布于 2024-09-27 05:42:32 字数 2370 浏览 2 评论 0原文

我对以下代码有疑问。

它使用 Mini-XML 库从 xml 文件中提取元素。

它编译并运行良好,只是它无法获取最后一个“半径元素”,而是返回 NULL(如果我不检查子节点是否不为 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);

是有问题的 XML 文件的一部分:

 <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>

这段代码有什么问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枯叶蝶 2024-10-04 05:42:32

好吧,我猜您正在谈论 Mini-XML,老实说我对此没有任何经验。我所看到的是,你的迭代器比你的名字领先一步。我认为您想要的更像是:

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);
}

一般来说,永远不要相信您拥有格式良好的 XML。即使您认为应该有一个孩子,也要始终检查 NULL。希望有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文