[xml] Copying node between documents



Hi,
I want to find a node in one document with xpath query and then to add set of nodes from another document as its children. I can't do it without loosing some memory (or generating sigsegv). I tried something like this:

xmlXPathContextPtr dest_ctx, src_ctx;
xmlXPathObjectPtr insert_point, src_set;
xmlNodePtr node;
int i;

dest_ctx = xmlXPathNewContext(dest_doc);
insert_point = xmlXPathEvalExpression("...", dest_doc);

src_ctx = xmlXPathNewContext(src_doc);
src_set = xmlXPathEvalExpression("...", src_ctx);

for (i = 0; i < src_set->nodesetval->nodeNr; i++) {
        xmlNodePtr node = xmlDocCopyNode(
                xpath_obj->nodesetval->nodeTab[i],
                dest_doc, 1 /* with children */ );
        xmlUnlinkNode(node);
        xmlAddChild(insert_point->nodesetval->nodeNr[0],
                node);
}


Valgrind show huge memory leaks, but adding xmlFreeNode(node) makes program crush. Where and how should I free memory? I call xmlFreeDoc() and xmlCleanupParser()...

Thanks
Pawel Palucha




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