tags:

views:

25

answers:

1

I am use libxml2 for XML parsing. The sample code on the libxml website are hard to follow and seem to lack some details. I arrived at the following code by Googling so I don't even think it's the proper way to do it, but it worked in the sample learning program I wrote, but not this one. I still don't know the proper way to use libxml in C++ so I'm running in the dark and hoping I hit something useful.

The XML file loads correctly and when this function outputs root->name correctly, but then when it goes through the children it just outputs text at cur->name and I don't know why. I have to put the counter in there to stop it from going into an infinite loop. I read somewhere white space in the XML file might cause this, but I don't know what to do. I just want the part name and ID.

xmlNode *cur = root;
cur = cur->xmlChildrenNode;

ofstream out;
out.open("errorlog.txt", ios::app);
out << "attempting reading current node\n";
out << "root: " << root->name << endl;

int counter = 0;

// advance until it hits stars
while(cur != NULL && counter < 10){
if ((!xmlStrcmp(cur->name, (const xmlChar *)"parts")))
    break;

    cur->next;
    counter++;
}

out << "counter: " << counter << endl;
out << "child: " << cur->name << endl;

This is the XML file I'm using:

<?xml version="1.0" encoding="utf-8"?>
<netlist>
    <parts>
        <part name="part10">
            <attribute name="id">1</attribute>
        </part>
        <part name="part20">
            <attribute name="id">2</attribute>
        </part>
        <part name="part30">
            <attribute name="id">3</attribute>
        </part>
    </parts>

    <junk>
        <stuff id="3" />
        <stuff id="4" />
        <stuff id="5" />
    </junk>
</netlist>
+1  A: 

The problem is that you are not advancing the current node in your while loop. Try changing cur->next; to cur = cur->next;. You are seeing the first child of <netlist> which is a text node containing the whitespace before the <parts> element.

D.Shawley
Oh crap, I feel so dumb. Thanks, that was it.
alex
No problem. There is a reason that I saw it quickly ;)
D.Shawley