Re: [xml] Child Traversing loop runs twice



On Wed, Feb 17, 2010 at 8:12 AM, andrew james
<andrew systemssingular com> wrote:
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.

No no, there's no need for that. xmlNode contains a "type" member.
Its values are from the xmlElementType enum. Just compare with
XML_ELEMENT_NODE or XML_TEXT_NODE.
See tree.h

xmlStrcmp is almost illogical for a human to read because it
evaluates to 0 when strings are the same

That's because xmlStrcmp is not a test for equality, but a comparison,
so it has to cater for three possible situations:
1. string1 is lexicographically "less" than string 2 (returns a value
less than zero)
2. strin1 is the same as string2 (returns 0)
3. string1 is lexicographically "greater" than string 2 (returns a
value greater than zero)

Hope this helps,
Csaba
-- 
Life is complex, with real and imaginary parts



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