开发者

Why does libxml2 output "text" on an element name (when it's not "text")?

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 n开发者_高级运维ode\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>


For the libxml2 , the next child node system for each node is a blank document by default , When using the DOM tree to parse the XML document, since the default way is to treat the space between nodes as the first child node, you can call the function xmlKeepBlanksDefault(0) to ignore the whitespace.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜