解析XML文件:根节点没有子节点 [英] Parsing XML files: Root Nodes has no child nodes

查看:88
本文介绍了解析XML文件:根节点没有子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从没使用过XML和XML解析器,我想为OpenGL的模型动画解析COLLADA文档.

I never worked with XML and XML parsers and i wanted to parse a COLLADA document for model animation with OpenGL.

我正在使用tinyxml2解析器,看来我在做错什么.

I am using the tinyxml2 parser and it seems that I am doing something wrong with that.

XMLDocument _doc;
_doc.LoadFile(path.c_str());

XMLNode* pRoot = _doc.FirstChild();

XMLNode* pElement =  pRoot->FirstChildElement("library_geometries");

我正在使用Xcode,并且在调试模式下,我可以看到pElement为NULL以及pRoot没有子节点.

I am working with Xcode and in debugging mode I can see, that pElement is NULL also that pRoot has got no child nodes.

推荐答案

tinyxml2 中,所有内容都是一个节点,而不仅仅是元素.因此, _doc.FirstChild()没有帮助,因为它会将您带到< COLLADA> 元素(可能是XML标头中的一个属性)之前的节点.您想要的是文档中的第一个子元素,即< COLLADA> ,然后是文档下面的第一个< library_geometries> 元素.试试这个:

In tinyxml2 everything is a node, not just elements. So _doc.FirstChild() is unhelpful as it's taking you to a node before the <COLLADA> element (probably an attribute in the XML header). What you want is the first child element in the document, i.e. <COLLADA> followed by the first <library_geometries> element below it. Try this:

#include "tinyxml2.h"
using namespace tinyxml2;
int main()
{
   XMLDocument doc;
   doc.LoadFile ("collada.xml");

   auto colladaElement = doc .FirstChildElement();
   auto lib_geomElement = colladaElement -> FirstChildElement("library_geometries");

   return 0;
}

而且,如果您想获得更多的C ++ 11/14体验,可以尝试使用我的 tinyxml2扩展将上述内容减少为:

And, if you want more of a C++11/14 experience you could try my tinyxml2 extension which reduces the above to:

#include "tixml2ex.h"
int main()
{
   tinyxml2::XMLDocument doc;
   doc.LoadFile ("collada.xml");
   auto lib_geomElement = find_element (doc, "COLLADA/library_geometries");

   return 0;
}

这篇关于解析XML文件:根节点没有子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆