[jdom-interest] Iterating through XML

Phil Weighill-Smith phil.weighill-smith at volantis.com
Tue Jul 6 05:21:07 PDT 2004


You can get the children of the Response element (your document root)
and iterate that list assuming your response only has inbound messages
in it (or you can apply an element filter to select only the inbound
message children). Alternatively (and perhaps "nicer"), you could use an
XPath to locate your inbound messages and iterate over the set of nodes
returned; this way you can handle the situation where a response can
contain other data too, or where you re-structure the response to have
additional layers within it, without the need to change your code
(assuming you use an appropriate XPath such as
"/descendant::InboundMessage"). See http://www.w3.org/TR/xpath for a
description of XPath.

JDOM has an integration with the jaxen XPath processor (which is a very
nice piece of technology).

Phil :n)

On Tue, 2004-07-06 at 12:34, Edd Dawson wrote:

> Hi 
> 
> I have got the following code :
> 
> Document doc = null;
> try {
> 	doc = builder.build(reader);
> } catch(Exception ex) {
> 	return "Error on making xml returned SAXable" + ex.getMessage();
> 		}
> 
> 
> 
> Which puts my inputStream XML nicely into the parser.
> 
> 
> Now i have the following in there :
> 
> <Response>
>    <InboundMessage>
>       <Ticket>1278</Ticket>
>       <MessageText>Example 1</MessageText>
>       <Phone>+4409878656787</Phone>
>       <Date>123456123</Date>
>    </InboundMessage>
>    <InboundMessage>
>       <Ticket>1279</Ticket>
>       <MessageText>Example 2</MessageText>
>       <Phone>+4409878656787</Phone>
>       <Date>123456123</Date>
>    </InboundMessage>
>    <InboundMessage>
>       <Ticket>1280</Ticket>
>       <MessageText>Example 3</MessageText>
>       <Phone>+4409878656787</Phone>
>       <Date>123456123</Date>
>    </InboundMessage>
> </Request>
> 
> 
> 
> I can't figure out from the manuals and docs that i have read how to
> iterate through all the instances of <InboundMessage> so i can retrieve
> the elements of each and bung them in a database.
> 
> Would anyone have any ideas how to do so?
> 
> thanks
> Edd
> 
> _______________________________________________
> To control your jdom-interest membership:
> http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost.com

-- 
Phil Weighill-Smith <phil.weighill-smith at volantis.com>
Volantis Systems
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://jdom.org/pipermail/jdom-interest/attachments/20040706/e3aa1066/attachment.htm


More information about the jdom-interest mailing list