开发者

Empty element causing libxml segfault

I have an xml file that goes roughly like this:

<root>
  <children>
    <child />
  </children>
  <otherchildren>
  </otherchildren>
  <morechildren>
    <child />
  </morechildren>
</root>

And here's the C that parses it:

doc = xmlDocPtr;
doc = xmlParseFile(file);
cur = xmlNodePtr;
cur = xmlDocGetRootElement(doc);
cur = cur->xmlChildrenNode;

while (cur != NULL){ // Loop through children of root

    // Do my thing

    cur = cur->next;
}

When the xmlNodePtr comes to othe开发者_开发百科rchildren, because there are no contents, the next node (the text node) is NULL

This gives me a problem, messes with my loop, and causes a segfault somehow.

How do I fix this other than the obvious if statement? There is more xml below otherchildren and I can't get it if the loop exits.


Using your .xml, I get:

./SOTrials ../Untitled1.xml 
../Untitled1.xml:10: parser error : expected '>'
</root>
^
error: could not parse file ../Untitled1.xml
Speicherzugriffsfehler

(Speicherzugriffsfehler = memory fault)

Changing the .xml to

...
</morechildren>
...

gives:

./SOTrials ../Untitled1.xml 
node type: Element, name: text
node type: Element, name: children
node type: Element, name: text
node type: Element, name: otherchildren
node type: Element, name: text
node type: Element, name: morechildren
node type: Element, name: text

Looks like your code is better than your xml.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜