0

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.

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.