Re: [xml] query - append DOM tree, identify leaf node



Senthil Nathan wrote:
1. Suppose if I have a pointer xmlDoc *doc1 to a DOM tree, built using xmlReadFile( ) or xmlParseFile( ) for file1.xml.
    Is it possible to add another DOM tree to this existing tree?
Like, if *doc2 points to another DOM tree of file2.xml, can this be appended to the *doc1.

Have a look at the tree module API documentation

xmlNodePtr xmlDocCopyNode(const xmlNodePtr node,
  xmlDocPtr doc,
  int extended)
http://www.xmlsoft.org/html/libxml-tree.html#xmlDocCopyNode

You can then append the node to its new parent in doc1 with xmlAddChild
http://www.xmlsoft.org/html/libxml-tree.html#xmlAddChild

2. Using xmlXPathEvalExpression( ), I could search for a tree structure exists in the DOM tree or not.
    But how do I find that is the leaf node.
<node>
          <n1>a</n1>
                <n11>abc</n11>
          <n2>b</n2>
    </node>

With leaf node I assume you mean an element node that has only text or attribute children but no other element children. A way of doing this in XPath is like this:

//*[count(./*)=0]

Which reads a select all elements E such that the count of element children of E is zero.

You can also traverse the tree with the tree api and check whether a node has no children of node-type element, but I find using XPath much more convenient

HTH
Lars.

--
Sun Microsystems                Lars Oppermann <lars oppermann sun com>
Nagelsweg 55                    Software Engineer
20097 Hamburg, Germany          Phone: +49 40 23646 959
http://www.sun.com/             Fax:   +49 40 23646 550
-----------------------------------------------------------------------
Sitz der Gesellschaft: Sun Microsystems GmbH, Sonnenallee 1,
D-85551 Kirchheim-Heimstetten, Amtsgericht Muenchen: HRB 161028
Geschaeftsfuehrer: Wolfgang Engels, Dr. Roland Boemer
Vorsitzender des Aufsichtsrates: Martin Haering



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