[jdom-interest] Problem with Iteration (again)

Per Norrman per.norrman at austers.se
Thu Jul 15 03:54:51 PDT 2004


Hi,

> 
>         // start with some root element...
>         Element oEleRoot = doc.getRootElement();
> 
>         // get down to Response element
>         Element oEleResponse = oEleRoot.getChild("Response");
> 
>         // get all children called InboundMessage
>            List oListInBoundMsg = null;
>         try {
>              oListInBoundMsg =
> oEleResponse.getChildren("InboundMessage");
>         } catch (Exception ex) {
>         return "error on making list : " + ex.getMessage();
>           }  

Response *is* the root element.

	Element oEleResponse = doc.getRootElement();

However, I recommend using XPath. In your case, you could simply
do:

    import org.jdom.xpath.XPath;
	
    ...

    List l = XPath.selectNodes(doc, "/Response/InboundMessage");
    for (Iterator iter = l.iterator(); iter.hasNext();) {
       Element e = (Element)iter.next();
       String ticket = e.getChildTextTrim("Ticket");
       ..
       ..
    }

You can also precompile the XPath expression if speed is essential and
this operation is frequent.

/pmn




More information about the jdom-interest mailing list