pgpython |
07-20-2009 11:32 AM |
adding xml files to dom libxml2
I am trying to write a c function which will append a xml file to a xmlDocument already opened. I am having two issues which I am not quite sure.
1. What happens if I try to add a node which already exists and how do I handle it
2. Every time I try and free either of the xmlDocPtr used it crashes.
What I have so far is:
Code:
int AppendDocToDom(xmlXPathContext *ctx, char * filename){
xmlDoc *new_dom = xmlReadFile(filename, NULL, XML_PARSE_NOERROR);
if (new_dom == NULL){
xmlFreeDoc(new_dom);
return XML_FILE_NOT_VALID;
}
xmlXPathContext *xpath_ctx = xmlXPathNewContext(new_dom);
xmlXPathObjectPtr xpath_object = xmlXPathEvalExpression((xmlChar *) "/path/to/root/node", xpath_ctx);
//Check the xml file appears to have the right structure by doing some xpath validation.
//Attempt to add the xml to the document.
xmlXPathObjectPtr exist_xpath_object = xmlXPathEvalExpression((xmlChar *) "/path/to/root/node", ctx);
xmlNode *root = exist_xpath_object->nodesetval->nodeTab[0];
xmlNode *child = xpath_object->nodesetval->nodeTab[0];
if (!xmlAddChildList(root, child)){
return CANNOT_ADD_XML;
}
xmlXPathFreeObject(xpath_object);
xmlXPathFreeContext(xpath_ctx);
xmlFreeDoc(new_dom);
return 0;
}
Can anyone explain where I am going wrong?
|