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

Kevin L. Cobb kevin.cobb at emergint.com
Thu Oct 27 11:03:03 PDT 2005


Hate to answer my own question, but as usual, I posted and then figured
it out a few minutes later. Seems the process of typing the problem into
an e-mail opens up the JDOM flood gates for me. 
 
Simply put, I needed to "detatch" the currentElement from the Document
and then add it to a new Docuement object. Then I could aply other XPath
expressions to this new Document. Here's the code snippet:
 
currentElement.detatch();
Docuement newDoc = new Document(currentElement);
 
-Kevin

________________________________

From: jdom-interest-bounces at jdom.org
[mailto:jdom-interest-bounces at jdom.org] On Behalf Of Kevin L. Cobb
Sent: Thursday, October 27, 2005 12:55 PM
To: jdom-interest at jdom.org
Subject: [jdom-interest] Question about List from Xpath.selectNodes(doc)


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
 
 
 

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


More information about the jdom-interest mailing list