0

XMLの解析に小さな問題があります。子ノードの名前を取得できません。

これが私のXMLコードです:

<?xml version="1.1" encoding='UTF-8'?>
<SceneObject>
<ParticleSystem>
</ParticleSystem>
</SceneObject>

XMLファイルを解析する方法は次のとおりです。

SceneObject::SceneObject(const char *_loadFromXMLFile, const char *_childType)
{
 xmlNodePtr cur;

 pXMLDocument = xmlParseFile( getPathForResource(_loadFromXMLFile, "xml") );

 if (pXMLDocument == NULL)
 {
  fprintf(stderr, "Document not parsed successfully. \n");
  return;
 }

 cur = xmlDocGetRootElement(pXMLDocument);

 if (cur == NULL) {
  fprintf(stderr, "Empty document\n");
  xmlFreeDoc(pXMLDocument);
  return;
 }


 if (!xmlStrEqual(cur->name, (const xmlChar *) "SceneObject"))
 {
  fprintf(stderr, "Document of the wrong type; root node == %s\n", cur->name);
  xmlFreeDoc(pXMLDocument);
  return;
 }

 SimpleLog("cur->name: %s", (const char*)cur->name);
 cur = cur->children;
 SimpleLog("cur->children->name: %s", (const char*)cur->name);
}

コンソールに表示されるのは次のとおりです。

cur->name: SceneObject
cur->children->name: text

「cur->children->name」が「text」であり、「ParticleSystem」ではないのはなぜですか?

何が間違っているので、どうすれば修正できますか?

ありがとう。

4

1 に答える 1

4

"text"ノードは、ドキュメント間の空白(改行文字<SceneObject><ParticleSystem>です 。この場合に必要なノードですcur->children->next<ParticleSystem>

一般typeに、ノードのメンバーを調べて、それが要素、テキスト、cdataなどであるかどうかを判断できます。

于 2010-02-18T00:52:06.767 に答える