[jdom-interest] Using Xpath and Element GetDocument
jaxtrx
jaxtrx at xde.net
Mon May 26 07:17:35 PDT 2003
Hi Mark,
Thanks much for the quick response.
Should have made a better example but didn't want to get too confusing.
Basically I want to be able to iterate through the different levels.
e.g. I have :
<root>
<top>
<job>
job parameters
</job>
<job>
job 2 parameters
</job>
</top>
</root>
And would like to use /root/top/job to retrieve a list of jobs - the
iterator seems to work, but I'm really after the XML below the element,
which I use to create a new instance of a document only containing a single
job element e.g.
> Element jobElem=(Element)jobIterator.next();
////New Code :
Document tmpDoc=new Document(jobElem.getDocument());
I think JDom is doing exactly as it's suppose to in reporting the XML for
the "attached" element, but I'm trying to create a sub document for
temporary use. I've tried using Document(Element) constructor, but had the
same getDocument() results
Well thanks for any insight, I'm going to try cloning nodes and removing
the root element from the validation and see what sort of trouble I can get
myself into :)
Charles
-----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