Re: [xml] create node from a string (with xml elements inside)



On Tue, Apr 19, 2011 at 05:40:12PM +0200, Sylvain Pointeau wrote:
Hello again,

I did it with parsing a doc in memory like

        const char * xml = "<mycontent><mytext>hello</mytext></mycontent>";

        xmlDocPtr doc_fragment = xmlReadMemory(xml,
strlen(xml),"noname.xml",NULL,0);

        xmlNodePtr root_node =        xmlDocGetRootElement(doc_fragment);

        xmlNodePtr new_node = xmlDocCopyNode (root_node, doc, 1);

        xmlFreeDoc(doc_fragment);

         xmlAddChild(mynodewhereIwanttoinsertthisfragment,new_node);


and it works!


is it the best solution? (only one?)

  I would rather suggest you use xmlParseInNodeContext() for this,
see include/libxml/parser.h
   http://xmlsoft.org/html/libxml-parser.html#xmlParseInNodeContext

 You will get back a node list to place at that location, it doesn't
try to insert automatically.

Daniel

-- 
Daniel Veillard      | libxml Gnome XML XSLT toolkit  http://xmlsoft.org/
daniel veillard com  | Rpmfind RPM search engine http://rpmfind.net/
http://veillard.com/ | virtualization library  http://libvirt.org/



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