You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@geronimo.apache.org by Chris Opacki <ch...@yahoo.com> on 2003/08/17 21:53:06 UTC

XPath and dom4j

I'm trying to grab a particular list of nodes using
dom4j from an xml document. Some things seem to work
and some obvious things don't. My first experience
with it is here..so it could be my lack of
understanding XPath.
I tried to get elements using this:
/ejb-jar/enterprise-beans
I'm expecting to get all of the child nodes directly
beneath /ejb-jar/enterprise-beans this way.

I am also trying to get a single attribute of a single
element using this: /ejb-jar[@version]

The only paths that seem to work are these: "//" or
"@version"

this is the code...

XPath xpathSelector = DocumentHelper.createXPath(str);
List nodes =
xpathSelector.selectNodes(node.getDocument());


What might I be doing wrong?

thanks.


__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com

Re: XPath and dom4j

Posted by James Strachan <ja...@yahoo.co.uk>.
I'd also recommend the excellent XPath tutorial, takes about 20 minutes 
to do and teaches you most things you need to know about XPath...

http://www.zvon.org/xxl/XPathTutorial/General/examples.html


On Sunday, August 17, 2003, at 11:30  pm, Jan Bartel wrote:

> Chris,
>
>> I'm trying to grab a particular list of nodes using
>> dom4j from an xml document. Some things seem to work
>> and some obvious things don't. My first experience
>> with it is here..so it could be my lack of
>> understanding XPath.
>> I tried to get elements using this:
>> /ejb-jar/enterprise-beans
>> I'm expecting to get all of the child nodes directly
>> beneath /ejb-jar/enterprise-beans this way.
> You should get a list of all enterprise-bean nodes with this 
> expression (note, should be just one of these in an ejb-jar 
> descriptor), not their children."/ejb-jar/enterprise-beans/*" should 
> get you all the children.
>
>
>> I am also trying to get a single attribute of a single
>> element using this: /ejb-jar[@version]
> Well, this xpath expression would return you the attribute "version" 
> of the "ejb-jar" element at the root of the document (that is, if 
> ejb-jar had such an attribute).
>
>> The only paths that seem to work are these: "//" or
>> "@version"
>> this is the code...
>> XPath xpathSelector = DocumentHelper.createXPath(str);
>> List nodes =
>> xpathSelector.selectNodes(node.getDocument());
> It might be easier to play around with the non-compiled XPath 
> expressions until you get it sorted out, eg:
>        List nodes = document.selectNodes ("/ejb-jar/enterprise-beans");
>        nodes = document.selectNodes ("/ejb-jar/enterprise-beans/*");
>
>
>
> cheers,
> Jan
>
>

James
-------
http://radio.weblogs.com/0112098/


Re: XPath and dom4j

Posted by Jan Bartel <ja...@mortbay.com>.
Chris,

> I'm trying to grab a particular list of nodes using
> dom4j from an xml document. Some things seem to work
> and some obvious things don't. My first experience
> with it is here..so it could be my lack of
> understanding XPath.
> I tried to get elements using this:
> /ejb-jar/enterprise-beans
> I'm expecting to get all of the child nodes directly
> beneath /ejb-jar/enterprise-beans this way.
You should get a list of all enterprise-bean nodes with this expression 
(note, should be just one of these in an ejb-jar descriptor), not their 
children."/ejb-jar/enterprise-beans/*" should get you all the children.


> I am also trying to get a single attribute of a single
> element using this: /ejb-jar[@version]
Well, this xpath expression would return you the attribute "version" of 
the "ejb-jar" element at the root of the document (that is, if ejb-jar 
had such an attribute).

> 
> The only paths that seem to work are these: "//" or
> "@version"
> 
> this is the code...
> 
> XPath xpathSelector = DocumentHelper.createXPath(str);
> List nodes =
> xpathSelector.selectNodes(node.getDocument());
It might be easier to play around with the non-compiled XPath 
expressions until you get it sorted out, eg:
        List nodes = document.selectNodes ("/ejb-jar/enterprise-beans");
        nodes = document.selectNodes ("/ejb-jar/enterprise-beans/*");



cheers,
Jan