[jdom-interest] Iterating through XML

Phill_Perryman at Mitel.COM Phill_Perryman at Mitel.COM
Tue Jul 6 06:13:22 PDT 2004

If there are a lot of elements in the list then the following from Brad I 
found useful. 

My XML file had 7000 child entries and the following reduced the build 
time from 0.9 seconds (which the user noticed) to below 0.1 second which 
they did not notice.

Don't use getChildren (which BTW was renamed to getChildElements in the 
cvs).  The list returned by getChildren is both filtered, to show only
Elements, and "live". "live" meaning changes to the list are reflected in 
element's underlying content.  One problem is a element has no way of 
how many child elements it has or were they are in the content list with
scanning the underlying content list and counting. So size() in the line

   for (int i = 0; i < element_list.size (); i ++)

causes element_list's actual content list to be scanned on each call, 
Elements and skipping any Text, Comment, or ProcessingInstruction node.
Similarly element_list.get(i) cann't determine where the i th Element is
without starting from index 0 in the underlying content list and scanning 
it finds the i th Element or runs out of nodes.

Iterators because a certain state must be maintained between a call to 
hasNext() and next() have their own slew of problems. If your interested,
search the archives for ConcurrentModificationException.

A solution to your problem in this case is to use getContent instead of
getChildren since size() is known (not calculated) by the parent element
and get(index) references the actual content list (not a filtered 

  private void transform_document (Element parent_element) {
    List list = parent_element.getContent();
    int size = list.size();
    for (int i = 0; i < size; i ++) {
      Object node = list.get(i);
      if (node instanceof Element) {
          Element element = (Element) obj;
          if (element.getNamespace() == orange_namespace) {
            //  do something ....
            //  replace old element
            list.set(i, new_element);

IS Dept, Software Engineer.
phill_perryman at mitel.com
Tel: +44 1291 436023

Laurent Bihanic <laurent.bihanic at atosorigin.com>
Sent by: jdom-interest-admin at jdom.org
06/07/2004 13:17

        To:     Edd Dawson <E.J.Dawson at derby.ac.uk>
        cc:     jdom-interest at jdom.org
        Subject:        Re: [jdom-interest] Iterating through XML

for (Iterator 
i.hasNext(); )
     Element inboundMessage = (Element)(i.next());



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" + 
>                                }
> 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

                 (o o)
Laurent Bihanic                | Tel: +33 (0)1 (direct)
AtosOrigin Systems Integration |      +33 (0)1
Generic Solutions / SC         | Fax: +33 (0)1
Les Miroirs - Bat. C           |
18, avenue d'Alsace            |
F-92926 La Defense Cedex       | E-mail: laurent.bihanic at atosorigin.com

Plonge tes racines dans la terre
Laisse nous vivre avec le vent
Passe l'hiver comme les graines
Et chante au printemps comme les oiseaux

The opinions expressed are entirely my own and may not necessarily be
those of my employer.  Also, I am not now nor have I ever been a
lawyer.  My opinions are provided as-is with absolutely no warrantee of
merchantability or fitness for any particular use.  Besides, you can't
prove I typed this.  No body saw me type this.  Who says I typed this?
To control your jdom-interest membership:

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://jdom.org/pipermail/jdom-interest/attachments/20040706/f5072f55/attachment.htm

More information about the jdom-interest mailing list