[jdom-interest] Iterating through XML
Patrick Simon
simon at staatsanzeiger-online-logistik.de
Tue Jul 6 05:22:04 PDT 2004
Hi Edd,
when starting with JDOM I had kind of the same (understanding)issue:
think of the *getChildren*() Method. It can do the job.
Here's a proposal snippet how I would do it
(this is my first 'helping-out-submittal' - To the pros on this list:
please correct me (don't beat me) if accidently I am saying sthg
wrong...;-))
// 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 = oEleResponse.getChildren("InboundMessage");
// create Iterator
Iterator oIterator = oListInBoundMsg.iterator();
Element oEleTicket;
Element oEleMessageText;
Element oElePhone;
Element oEleDate;
//Loop:
while (oIterator.hasNext()) {
//derive Reference from current iterator
Element oEleNext = (Element) oIterator.next();
oEleTicket = oEleNext.getChild("Ticket");
oEleMessageText = oEleNext.getChild("MessageText");
//...more Elements
// call method to do sthg with the Elements data
strStatus=furtherProcessElements(oEleTicket, oEleMessageText /*...*/);
// or even:
// strStatus=processInBoundMsg(oEleNext);
// requires some Status+error handling but not to forget...
// if anything goes wrong: LEAVE THE LOOP
if (!(strStatus.equals(strOK))) {
break;
}
}// while...
hope this is of help to you...
cu
pat
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
>
>
>
>
>
More information about the jdom-interest
mailing list