user2485337
user2485337

Reputation: 51

libxml2: missing children when dumping a node with xmlNodeDump()

I'm facing an issue with libxml2 (version 2.7.8.13). I'm attempting to dump a node while parsing an in-memory document with a xmlTextReaderPtr reader.

So upon parsing the given node, I use xmlNodeDump() to get its whole content, and then switch to the next node. Here is how I proceed:

[...]
// get the xmlNodePtr from the text reader
node = xmlTextReaderCurrentNode(reader);

// allocate a buffer to dump into
buf = xmlBufferCreate();

// dump the node
xmlNodeDump(buf, node->doc, node, 0 /* level of indentation */, 0 /* disable formatting */);

result = strdup((char*)xmlBufferContent(buf));

This works in most cases, but sometimes the result is missing some children from the parsed node. For instance, the whole in-memory xml document contains

[...]
<aList>
  <a>
    <b>42</b>
    <c>aaa</c>
    <d/>
  </a>
  <a>
    <b>43</b>
...
</aList>

and I get something like:

<aList>
  <a>
    <b>42</b>
    </c>
  </a>
</aList>

The result is well formed but it lacks some data ! A whole bunch of children has "disappeared". xmlNodeDump() should recursively dumps all children of .

It looks like some kind of size limitation. I guess I do something wrong, but I can't figure out what.

Thank you for your answers.

Upvotes: 1

Views: 2006

Answers (1)

user2485337
user2485337

Reputation: 51

I succeeded in implementing this correctly another way, still I do not understand what happened there. Thank you for having read my question.

FYI, instead of trying to tinker an existing parsing code based on xmlTextReader, I have just rewritten a small parsing module for my case (dump all the 1st level siblings into separate memory chunks). I did so by using the parsing and tree modules of libxml2, so:

  1. get the tree from the in-memory xml document with xmlReadMemory()
  2. get the first node with xmlDocGetRootElement()
  3. for each sibling (with xmlNextElementSibling() ), dump its content (all children recursively) with xmlNodeDump()

Et voilà, kinda straightforward actually. Sometimes it's easier to start from scratch...

I guess there was some side effect.

Upvotes: 1

Related Questions