Which function do I use to retrieve a whole hierarchy?

S

Scott Simpson

I can build portions of DOM trees with jdom and xpathapi and using "new
Element()" and "appendChild()" or "addContent()". Which function do I
use to get a whole portion of a DOM tree though? For example, suppose I have

<foo>
<bar name="dopey">
<snafu>
Text
</snafu>
</bar>
<bar name="sleepy">
<snafu>
More text
</snafu>
</bar>
</foo>

I can use an XPath query like
"/foo/bar[@name="dopey"]/ancestor-or-self::*" but that will give me back
a *list* of all the nodes it found. I want a pointer to the top <bar>
node and I want the rest of the stuff to be in a hierarchy below that
just like it is in the XML file. Is there a way to retrieve a hierarchy?
Thank you.
 
D

David Carlisle

Scott Simpson said:
I can build portions of DOM trees with jdom and xpathapi and using "new
Element()" and "appendChild()" or "addContent()". Which function do I
use to get a whole portion of a DOM tree though? For example, suppose I have

<foo>
<bar name="dopey">
<snafu>
Text
</snafu>
</bar>
<bar name="sleepy">
<snafu>
More text
</snafu>
</bar>
</foo>

I can use an XPath query like
"/foo/bar[@name="dopey"]/ancestor-or-self::*" but that will give me back
a *list* of all the nodes it found. I want a pointer to the top <bar>
node and I want the rest of the stuff to be in a hierarchy below that
just like it is in the XML file. Is there a way to retrieve a hierarchy?
Thank you.

The Xpath to the top of the document is / which returns the root node
(document node)

I'm not sure what you mean by teh "top <bar> node as there are several
sibling bar nodes in your example.
/foo would return teh top level element
/foo/bar[1] would return the first of the bar nodes.

Once you have the node returned you can traverse the hierarchy below
that point using dom cals in your application or further Xpaths or
whatever.

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top