[jdom-interest] Using Xpath and Element GetDocument

jaxtrx jaxtrx at xde.net
Mon May 26 07:26:29 PDT 2003


Mark,

Sort of answered my own question with your help, just needed to clone the
node to seperate it from the original document.  Thanks for the tip on //
being slower - now I have to go read about xpath :)


            XPath jobPath = XPath.newInstance("/root/job");
            List jobList = jobPath.selectNodes(activeDoc);
            Iterator jobIterator=jobList.iterator();
            while(jobIterator.hasNext()){
                System.out.println("------------------");
                Element jobElem=(Element)jobIterator.next();

                XMLOutputter outputter = new XMLOutputter();
                outputter.setExpandEmptyElements(true);
                Document tmpDoc= new Document((Element)jobElem.clone());
                try{
                    outputter.output(tmpDoc, System.out);
                }catch(Exception e){
                    e.printStackTrace();
                }

            }

-----Original Message-----
From: jdom-interest-admin at jdom.org
[mailto:jdom-interest-admin at jdom.org]On Behalf Of MarkW
Sent: Monday, May 26, 2003 8:03 AM
To: jaxtrx at xde.net
Cc: Jdom-Interest
Subject: RE: [jdom-interest] Using Xpath and Element GetDocument


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

_______________________________________________
To control your jdom-interest membership:
http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhos
t.com




More information about the jdom-interest mailing list