Re: [xml] using xmlDocSetRootElement to move nodes between documents



Daniel Veillard wrote:
On Tue, Jul 27, 2004 at 07:11:53PM +0200, Martijn Faassen wrote:

is moving nodes between documents in general not supported this way?
Since doc1 and doc2 are not sharing their dictionnary, yes you have a problem.
If you're starting to move nodes between documents like that you can either
desactivate dictionnaries or make sure they share dictionnaries. Use one of
the more recent APIs like xmlReadDoc() and set XML_PARSE_NODICT option.

I've just tried a very simple test case and set the XML_PARSE_NODICT option for both the source and target document. I've also thrown xmlReconciliateNs() in for good measure. :)

#include "libxml/tree.h"
#include "libxml/parser.h"

int xmlImportNode(xmlDoc* doc, xmlNode* node) {
  return xmlReconciliateNs(doc, node);
}

int main() {
  xmlDoc* doc1;
  xmlDoc* doc2;
  xmlNode* node;
  xmlChar* text = "<doc>foo</doc>";
  char* url = "http://www.infrae.com";;
  doc1 = xmlReadDoc(text, url, NULL,
                    XML_PARSE_NOENT | XML_PARSE_NODICT | XML_PARSE_NONET);
  doc2 = xmlReadDoc(text, url, NULL,
                    XML_PARSE_NOENT | XML_PARSE_NODICT | XML_PARSE_NONET);
  node = xmlDocGetRootElement(doc1);
  xmlImportNode(doc2, node);
  xmlDocSetRootElement(doc2, node);
  xmlFreeDoc(doc1);
  xmlFreeDoc(doc2);
}

This still gives me valgrind invalid reads when doing the xmlFreeDoc().

Regards,

Martijn



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