Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 07-20-2009, 11:32 AM   #1
Registered: Dec 2005
Location: Sheffield, UK
Distribution: Gentoo
Posts: 142

Rep: Reputation: 32
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:

int AppendDocToDom(xmlXPathContext *ctx, char * filename){
     xmlDoc *new_dom = xmlReadFile(filename, NULL, XML_PARSE_NOERROR);
    if (new_dom == NULL){
        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;
    return 0; 
Can anyone explain where I am going wrong?


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
XML parsing using DOM & Xerces on C++ amitpardesi Linux - Software 0 01-21-2009 05:15 PM
LXer: Dynamic XML document construction with the PHP DOM LXer Syndicated Linux News 0 01-25-2007 06:03 PM
C++ XML DOM parser? pembo13 Programming 1 02-05-2006 03:16 PM
CXOffice can't find XML::DOM BroX Slackware 0 10-25-2004 02:47 AM
XML php dom amirspl Programming 2 09-20-2003 06:04 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:02 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration