LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 07-20-2009, 12:32 PM   #1
pgpython
Member
 
Registered: Dec 2005
Location: Sheffield, UK
Distribution: Gentoo
Posts: 141

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:

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?
 
  


Reply


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 06:15 PM
LXer: Dynamic XML document construction with the PHP DOM LXer Syndicated Linux News 0 01-25-2007 07:03 PM
C++ XML DOM parser? pembo13 Programming 1 02-05-2006 04:16 PM
CXOffice can't find XML::DOM BroX Slackware 0 10-25-2004 03:47 AM
XML php dom amirspl Programming 2 09-20-2003 07:04 AM


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

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