[jdom-interest] Using Xpath and Element GetDocument

jaxtrx jaxtrx at xde.net
Mon May 26 08:11:23 PDT 2003


Now trying to use List jobList = doc.getRootElement().getChildren(); but get

java.lang.IllegalStateException: Root element not set
        at org.jdom.Document.getRootElement(Document.java:181)
        at com.rampantxde.Job.resource.jdomtest.main(jdomtest.java:208)

Even though I do have a root element, even named root ;)


<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>

Which is created via the below :

            XPath jobPath = XPath.newInstance(loadChild);
            List jobList = jobPath.selectNodes(doc);
            //System.out.println("getChildUnits:This "+loadChild+" has "+
jobList.size() +" list items");
            Iterator i = jobList.iterator();
            Document tmp=null;
            Element root = null;

            while (i.hasNext()) {
                Element servlet = (Element) i.next();
                String name=servlet.getName();
//Reports 2 job's added, confirms the XML output
                System.out.println("loadChild="+loadChild+" Adding
"+name+"="+servlet);
                if(root==null)
                    root=new Element("root");
                root.addContent((Element)servlet.clone());
            }

            if(root==null)
                return null;
            tmp=new Document(root);

Any ideas on what I did wrong ?  Other methods that normally would report a
root element not found type message still work fine.



-----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