[jdom-interest] Question about List from Xpath.selectNodes(doc)

Kevin L. Cobb kevin.cobb at emergint.com
Thu Oct 27 09:54:39 PDT 2005

I'm using the XPath functionality to set an XPATH String and then call
the selectNodes(sourceDocument) to get a list of Content back based on
the XPATH. When I iterate over the List of objects in the List (all
should be Element objects), I can see that everything is as expected.
However, when I take one of the Element objects in the List and convert
it to a Document, I get the entire Docuement back, versus just the
Element I am using and all of its children. The code looks like this:
XPath myXpath = XPath.newInstance("xpath to several elements downstream
of the root");
Element currentElement = null;
Iterator itr = myXpath..selectNodes(doc).iterator();     //doc is my XML
while (itr.hasNext()) {
    currentElement = (Element) itr.next();
    //Iterating over the currentElement's children gives me what I
expect, all child elements names are output
    Iterator itr = currentDiagnosis.getChildren().iterator();
    while (itr.hasNext()) {
       System.out.println(((Element) itr.next()).getName());

    //But putting the currentElement to a Document gives me my original
Document back. Wierd to me but perhaps this is by design.
    Format format = Format.getPrettyFormat();
    XMLOutputter output = new XMLOutputter(format);
Assuming this behavior is by design, how can I get a Document object of
my Elements in order to write XPath expressions on these separatly?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.jdom.org/pipermail/jdom-interest/attachments/20051027/61184697/attachment.htm

More information about the jdom-interest mailing list