You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-dev@xerces.apache.org by Gadi Shiloah <ga...@managair.net> on 2002/02/11 18:30:50 UTC
DOM_Element::getElementsByTagName() question
Hello all,
I am a bit new at this, but here goes...
I have a sub-tree that looks like this:
<parent>
<child0 data="x"/>
<child1 data="y"/>
<child2 data="z"/>
<!-- continues to childN -->
</parent>
My goal is to obtain the contents of the data attribute of each child.
In order to do so, I use the following code:
DOM_Element parentElem; //refers to <parent>
DOM_Element childElem;
DOM_NodeList List;
DOMString strTag; //to hold the sought tag name
//strTag is initialized to "child0"
List=parentElement.getElementsByTagName(strTag);
while(List!=0) //(i hoped...) List will turn null at the 1st time strTag matches no tag name
{
childElement=(DOM_Element &)List.item(0);
//get the attribute's value and do something
//strTag is assigned the tag name of the next expected child i.e. child1, then child2, etc.
List=parentElement.getElementsByTagName(strTag);
}
Unfortunately, List does not become null when i call
List=parentElement.getElementsByTagName(strTag);
with an invalid strTag, and while's condition never evaluates to false, and
I get an exception thrown pretty soon after that happens.
The documentation says only this:
DOM_NodeList DOM_Element::getElementsByTagName (const DOMString & name)const
Returns a NodeList of all descendant elements with a given tag name, in the order in which they would be encountered in a preorder traversal of the DOM_Element tree.
Parameters:
name The name of the tag to match on. The special value "*" matches all tags.
Returns:
A list of matching DOM_Element nodes.
I thought of using an additional DOM_Node and checking it's type at the beggining of each iteration, breaking once it is not ELEMENT_NODE.
But isn't there another way to tell if the sought child node was found?
Thanks in advance, Gadi.