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

Hi Lars,
Thanks. xmlDocCopyNode would help me to append DOM trees.
And, //*[count(*)=0] this checks for the leaf node.
btw, you were also saying about using the Tree API's we can traverse the tree and search.
Could you please let me know, how do I search for whether the following tree structure
exists in the DOM tree using TREE API's,  "//node//n1" or "//node//n1//n11".
I'm new to use the API's available in libxml2. Just learning and using those stuff.

On 8/23/07, Lars Oppermann <Lars Oppermann sun com> wrote:
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)

You can then append the node to its new parent in doc1 with 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:


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


Sun Microsystems                Lars Oppermann <lars oppermann sun com >
Nagelsweg 55                    Software Engineer
20097 Hamburg, Germany          Phone: +49 40 23646 959             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]