C++:解析 XML 文件时出现问题(MXML 库)
我对以下代码有疑问。
它使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我猜您正在谈论 Mini-XML,老实说我对此没有任何经验。我所看到的是,你的迭代器比你的名字领先一步。我认为您想要的更像是:
一般来说,永远不要相信您拥有格式良好的 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:
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.