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 "Leitner, Sarah M. [Contractor]" <le...@spawar.navy.mil> on 2002/12/27 18:03:12 UTC

Using Xerces - questions

To all,

I have a doc that I am trying to go through recursively and find every child
using DOM. THen, if I want the child, I will put it into one of two or three
linked lists or vectors. 

My doc that I am parsing is something like this:
<?xml version="1.0"?><Title><x1>value1<a>"a value"</a><b>"another
value"</b></x1><x2>value2<aa>"aa value"</aa></x2><Title>

DOMDocument * doc = parser->getDocument();
DOMElement * elm = doc->getDocumentElement();
const XMLCh* nodeName = elm->getNodeName();

DOMNodeList * list = elm->getChildNodes();
int x = list->getLength();
int y = 0;
while (y < x) {

   cout << "child " << y << " is " <<
XMLString::transcode(list->item(y)->getNodeName())<< endl;
 
   DOMNode * grandChild = list->item(y)->getFirstChild();

   if (!grandChild->hasChildNodes()){
       //this is where to get the child nodes, but so far I just get the
same children again.
       //so I think there is a big error here

       DOMNode * grandChild2 = list->item(y)->getFirstChild();
       cout << "Child " << y << " has a node value of " <<
XMLString::transcode(grandchild->getNodeValue()) << endl;
       //then, more of the same to get the great Grandchildren....

   }
    y++;
}

Thanks much for all of your help and have a great Holiday!

So, how do I set it up recursively to get all the children of children?
Also, I haven't gotten attributes or no whitespace to work yet, so my
document is still mushed all together.

Sarah

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org


Re: Using Xerces - questions

Posted by Gareth Reakes <ga...@decisionsoft.com>.
Hi,
	have a look at DOMNodeIterator and DOMTreeWalker. They provide 
easy recursive access to the tree.


> while (y < x) {
> 
>    cout << "child " << y << " is " <<
> XMLString::transcode(list->item(y)->getNodeName())<< endl;
>  
>    DOMNode * grandChild = list->item(y)->getFirstChild();
> 
>    if (!grandChild->hasChildNodes()){
>        //this is where to get the child nodes, but so far I just get the
> same children again.
>        //so I think there is a big error here
> 
>        DOMNode * grandChild2 = list->item(y)->getFirstChild();

I think you want to say

	DOMNode *grandChild2 = grandChild->getFirstChild();


>        cout << "Child " << y << " has a node value of " <<
> XMLString::transcode(grandchild->getNodeValue()) << endl;
>        //then, more of the same to get the great Grandchildren....
> 
>    }
>     y++;
> }

> So, how do I set it up recursively to get all the children of children?

Set up a recursive method or take a look at the NodeIterator/TreeWalker.

> Also, I haven't gotten attributes or no whitespace to work yet, so my
> document is still mushed all together.

Can you expand on what you are doing here?



Gareth


-- 
Gareth Reakes, Head of Product Development  +44-1865-203192
DecisionSoft Limited                        http://www.decisionsoft.com
XML Development and Services




---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org