tags:

views:

137

answers:

1

Hello,

I've got a small problem with parsing XML. I cannot get the name of a child node.

Here's my XML code:

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

Here's how I parse the XML file:

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

What I get in the console is:

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

Why "cur->children->name" is "text" and is not "ParticleSystem"?

What am I doing wrong and how can I fix this?

Thanks.

+3  A: 

The "text" node is the whitespace (newline character) between <SceneObject> and <ParticleSystem> in your document. cur->children->next is the <ParticleSystem> node you want in this case.

In general you can consult the type member of a node to determine whether it is an element, text, cdata, etc.

ccmonkey