[jdom-interest] Using Xpath and Element GetDocument

jaxtrx jaxtrx at xde.net
Mon May 26 05:46:21 PDT 2003


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>




More information about the jdom-interest mailing list