[jdom-interest] does XPath implementation work correctly?

bob mcwhirter bob at werken.com
Sun Apr 29 10:52:16 PDT 2001

On Sun, 29 Apr 2001, Robert Koberg wrote:

> Thanks for the basic explanation. I am new to java... I am trying to see
> what benefit I can achieve by replacing some things I currenlty do with XSLT
> with java manipulations. I have been told that JDOM is the way to go (???).
> There is definitely a steep learning curve...

W3C spec to truly understand XPath.

JDOM API to understand the results of applying an XPath.

Just realize that werken.xpath always returns a "NodeSet" as per
the W3C spec.  And that the "nodes" in the "NodeSet" are objects
from the org.jdom.* hierarchy.

> Is there any documentation that will help me get through the 'basic'
> questions for JDOM and XPath within JDOM? I can only find the API's and I
> could really use some examples of what I need to do.

It's my feeling that if you understand how XPath works, in the generic
sense, then werken.xpath + JDOM should be fairly transparent.

> Two main questions I have are:
> 1. What do I need to do to get the value of the attribute after applying
> this expression:
> XPath xpath = new XPath("//*[@id='docs_000000001']");

Well, you'll get back a List of Elements.  Then, use methods on the
org.jdom.Element object to do whatever you want.  (getText(), etc).

btw, that xpath will return Elements and not Attributes.
(the @id attribute is used only as a predicate to 'filter' the
Elements selected by //*)

> 2. After getting the result "[[Element: <section/>]]" using the expression:
> XPath xpath = new XPath("//*[@id='docs_000000001']");
> How can I add the following:
> <page>
>    <pointer fileref="foo.gif"/>
>     <floaters/>
> </page>

Use org.jdom.Element methods to do whatever you like.

> -- Is there a more direct way to insert this chunk of XML into the exact
> position I need (without XPath)?

Sure, just Element.getChild() until you find the insertion point, and
then insert the new fragment.

> I will be glad to put your work through its paces if I can get over these
> basic hurdles.

Really, read the W3C spec first, to truly understand the basics of
what an XPath implementation (of any sort) should be doing.  It'll
make life a lot easier.

The basic premise is that XPath is a selection language, to 
select portions of your Document.  It doens't automagically do
string representations or anything.  It just helps navigate into
your document.  

Once you've navigated, you're back to playing with the JDOM
library itself.


More information about the jdom-interest mailing list