[jdom-interest] Using Xpath and Element GetDocument

MarkW mark.wilson at wilsoncom.de
Mon May 26 06:03:11 PDT 2003


Hi,
why not use:

List jobList = doc.getRootElement().getChildren();

Also note using an xpath expression like "//job" is relatively slow
(especially with deep structured trees). Better is "/root/job"

Greetings,
Mark

> -----Original Message-----
> From: jdom-interest-admin at jdom.org 
> [mailto:jdom-interest-admin at jdom.org] On Behalf Of jaxtrx
> Sent: Monday, May 26, 2003 2:46 PM
> To: jdom-interest at jdom.org
> Subject: [jdom-interest] Using Xpath and Element GetDocument
> 
> 
> Hi All,
> 
> Hoping for some help iterating through a list of jobs as 
> shown in the XML
> and code below.
> Basically no matter what combination of Xpath filters I use, 
> the Element
> getDocument method always seems to return the original 
> document text 2 times
> as there are 2 job nodes.   I'm ideally looking for output 
> similiar to the
> expected output section with only the single root job per 
> element iterated.
> 
> I'm assuming that getDocument is tied to the original parent 
> element (root),
> but is there a way of just getting the child XML or XML 
> specific to the
> current Element?
> 
> Thanks much in advance!
> 
> Charles Havranek
> 
> 
> 
> 
>             XPath jobPath = XPath.newInstance("//job");  
> //Have tried //job,
> //root/*, //job//*, //root//job
>             List jobList = jobPath.selectNodes(doc);
>             Iterator jobIterator=jobList.iterator();
>             while(jobIterator.hasNext()){
>                 Element jobElem=(Element)jobIterator.next();
>                 XMLOutputter outputter = new XMLOutputter();
>                 outputter.setExpandEmptyElements(true);
>                 try{
>                     outputter.output(jobElem.getDocument(), 
> System.out);
>                 }catch(Exception e){
>                     e.printStackTrace();
>                 }
>             }
> Source XML:
> <?xml version="1.0" encoding="UTF-8"?>
> <root><job>
> 
>     <input>inputReader</input>
>         <proc>cleanData</proc>
>         <proc>mailMessage</proc>
>         <proc>DisplayDataUnit</proc>
>     <output>outputWriter</output>
> 
>     </job><job>
> 
>     <input>inputReaderStep2</input>
>         <proc>cleanData</proc>
>         <proc>mailMessage</proc>
>     <output>outputWriter</output>
> 
>     </job></root>
> 
> Problem Output :
> Same as Source XML 2 times.
> 
> Expected Output :
> -------------------------JOB 1
> 
>     <input>inputReader</input>
>         <proc>cleanData</proc>
>         <proc>mailMessage</proc>
>         <proc>DisplayDataUnit</proc>
>     <output>outputWriter</output>
> 
>     </job>
> 
> -------------------------JOB 2
> 
>     <input>inputReaderStep2</input>
>         <proc>cleanData</proc>
>         <proc>mailMessage</proc>
>     <output>outputWriter</output>
> 
>     </job>
> 
> _______________________________________________
> To control your jdom-interest membership:
> http://lists.denveronline.net/mailman/options/jdom-interest/yo
uraddr at yourhost.com




More information about the jdom-interest mailing list