Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » JDOM User »

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

Kevin L. Cobb

2005-10-27


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 Document
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);
    System.out.println(output.outputString(currentElement.getDocument()));
}
 
 
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?
 
 
Thanks,
 
Kevin
 
 
 

 
_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/youraddr@(protected)
©2008 junlu.com - Jax Systems, LLC, U.S.A.