Adrian,

getChildNodes() will return a pointer to DOMNodeList and it may contain
any type of the nodes (see DOMNode interface:
http://xerces.apache.org/xerces-c/apiDocs/classDOMNode.html). If you're
only interested in processing the element nodes than you should check
the node type to see if it's an element node. 

Node interface is the primary datatype for the entire DOM. There are 12
different enumerated types available for any nodes (see above link). The
standard states that "The attributes nodeName, nodeValue and attributes
are included as a mechanism to get at node information without casting
down to the specific derived interface. In cases where there is no
obvious mapping of these attributes for a specific nodeType (e.g.,
nodeValue for an Element or attributes for a Comment), this returns
null.". That's where your null values are coming from.

Cheers,


-Ozgur Sahoglu


-----Original Message-----
From: Adrian Schubert [mailto:[EMAIL PROTECTED] 
Sent: Monday, February 18, 2008 10:43 AM
To: [email protected]
Subject: What are 'null' and 'non-element' children?

Hi people
 
I've been studying and using some pieces of the sample code delivered 
with xerces.
There's something I don't understand about the DOM parsing. When I'm 
traversing the DOM, looking for a particular node with a given name, 
each time I have to filter out the nodes that are NOT elements, and NOT 
null.
In other words, the children returned by DOMNodeList* getChildNodes() 
are not just the nodes I can see when looking at my XML file, but some 
further mysterious null and non-element nodes. Aren't nodes simply the 
elements we can see in the XML file, for example <imageDescription> or 
<fileSize>?
Here's how I get the children of a node:

   DOMElement* currentElement = elementRoot; // initialize to root
   children = currentElement->getChildNodes();
   nChildren = children->getLength(); // this number includes null- and 
non-elements!

Even when my parent element contains only 2 children that I know of, I 
get, for example, 5 children back according to children->getLength.
To get what I really want I have to test to see which ones are the 
"regular" elements by doing:

        if(!currentNode->getNodeType() ||  // is NULL
            currentNode->getNodeType() != DOMNode::ELEMENT_NODE ) // 
isn't an element
        {
           continue; // check next node
        }

Why is this? What are these mysterious invisible children in the XML 
file? Is there a better way to find one or more elements that have a 
known location in the DOM for an XML file?

Thanks, Adrian

Reply via email to