geronimo-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jan Bartel <>
Subject Re: XPath and dom4j
Date Sun, 17 Aug 2003 22:30:59 GMT

> 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 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/*");


View raw message