Re: [xml] Child Traversing loop runs twice



siddharth sehgal wrote:
Hi

I am new to libxml2 and intend to use it in my application which will run on embedded windows/ linux. I was trying to get hold of some common xml parsing routines and there is an interesting observation which I cannot resolve. although the xml I am parsing is a bit more elaborative but the observation persists on simple xml like this also.

<?xml version="1.0" ?>
<CAST>
    <a />
    <b />
    <c />
</CAST>


xmlDocPtr doc;
xmlNodePtr topcur;
.
.
doc = xmlParseFile(ansiSTR);
.
.
topcur = doc->children;//xmlDocGetRootElement(doc);//At <CAST>
.
.
topcur=topcur->children;
while(topcur != NULL){
    printf("%s\n",topcur->name);
    topcur=topcur->next;
}

Output expected is:
a
b
c

Actual Output:
text
a
text
b
text
c
text

The loop for child traversing runs twice. I cant figure this out . Any suggestions will be deeply appreciated.

thanks
Sid


------------------------------------------------------------------------

_______________________________________________
xml mailing list, project page  http://xmlsoft.org/
xml gnome org
http://mail.gnome.org/mailman/listinfo/xml


Though I am inexperienced with the specifics of libxml2, I have used other parsers for html (xml), that also read what to us is like extra text nodes.

Anyone may want to correct this if I am wrong,

For some reason, someone else may know the reason, some parsers read text nodes between specific elements (is it the new line?).

for this particularity of parsers, the suggestion is that you add some conditions in the code to evaluate node name.

for your code that is... and someone may also know a better evaluation function, I used xmlStrcmp that I found in tutorials, normal char comparison fails, but xmlStrcmp is almost illogical for a human to read because it evaluates to 0 when strings are the same...note the ! needed to initiate an if statement that is name = text

if(!xmlStrcmp(topcur->name, (const xmlChar *) "text")) {
   // node is 'text'
}

if(xmlStrcmp(topcur->name, (const xmlChar *) "text")) {
   // node is not 'text'
}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]