<br><font size=2 face="Courier New">If there are a lot of elements in the list then the following from Brad I found useful. </font>
<br>
<br><font size=2 face="Courier New">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.</font>
<br>
<br><font size=2 face="Courier New">Don't use getChildren (which BTW was renamed to getChildElements in the current<br>
cvs). The list returned by getChildren is both filtered, to show only<br>
Elements, and "live". "live" meaning changes to the list are reflected in the<br>
element's underlying content. One problem is a element has no way of knowing<br>
how many child elements it has or were they are in the content list with<br>
scanning the underlying content list and counting. So size() in the line<br>
<br>
for (int i = 0; i < element_list.size (); i ++)<br>
<br>
causes element_list's actual content list to be scanned on each call, counting<br>
Elements and skipping any Text, Comment, or ProcessingInstruction node.<br>
Similarly element_list.get(i) cann't determine where the i th Element is<br>
without starting from index 0 in the underlying content list and scanning until<br>
it finds the i th Element or runs out of nodes.<br>
<br>
Iterators because a certain state must be maintained between a call to <br>
hasNext() and next() have their own slew of problems. If your interested,<br>
search the archives for ConcurrentModificationException.<br>
<br>
A solution to your problem in this case is to use getContent instead of<br>
getChildren since size() is known (not calculated) by the parent element<br>
and get(index) references the actual content list (not a filtered version).<br>
<br>
private void transform_document (Element parent_element) {<br>
List list = parent_element.getContent();<br>
int size = list.size();<br>
for (int i = 0; i < size; i ++) {<br>
Object node = list.get(i);<br>
if (node instanceof Element) {<br>
Element element = (Element) obj;<br>
<br>
if (element.getNamespace() == orange_namespace) {<br>
// do something ....<br>
// replace old element<br>
list.set(i, new_element);<br>
}<br>
<br>
transform_document(element);<br>
}<br>
}<br>
}</font><font size=2 face="sans-serif"><br>
<br>
/Phill<br>
IS Dept, Software Engineer.<br>
phill_perryman@mitel.com<br>
http://www.mitel.com<br>
Tel: +44 1291 436023</font>
<br>
<br>
<br>
<table width=100%>
<tr valign=top>
<td>
<td><font size=1 face="sans-serif"><b>Laurent Bihanic <laurent.bihanic@atosorigin.com></b></font>
<br><font size=1 face="sans-serif">Sent by: jdom-interest-admin@jdom.org</font>
<p><font size=1 face="sans-serif">06/07/2004 13:17</font>
<br>
<td><font size=1 face="Arial"> </font>
<br><font size=1 face="sans-serif"> To: Edd Dawson <E.J.Dawson@derby.ac.uk></font>
<br><font size=1 face="sans-serif"> cc: jdom-interest@jdom.org</font>
<br><font size=1 face="sans-serif"> Subject: Re: [jdom-interest] Iterating through XML</font></table>
<br>
<br>
<br><font size=2 face="Courier New"><br>
for (Iterator i=doc.getRootElement().getChildren("InboundMessage").iterator(); <br>
i.hasNext(); )<br>
{<br>
Element inboundMessage = (Element)(i.next());<br>
<br>
...<br>
}<br>
<br>
Laurent<br>
<br>
<br>
Edd Dawson wrote:<br>
<br>
> Hi <br>
> <br>
> I have got the following code :<br>
> <br>
> Document doc = null;<br>
> try {<br>
> doc = builder.build(reader);<br>
> } catch(Exception ex) {<br>
> return "Error on making xml returned SAXable" + ex.getMessage();<br>
> }<br>
> <br>
> <br>
> <br>
> Which puts my inputStream XML nicely into the parser.<br>
> <br>
> <br>
> Now i have the following in there :<br>
> <br>
> <Response><br>
> <InboundMessage><br>
> <Ticket>1278</Ticket><br>
> <MessageText>Example 1</MessageText><br>
> <Phone>+4409878656787</Phone><br>
> <Date>123456123</Date><br>
> </InboundMessage><br>
> <InboundMessage><br>
> <Ticket>1279</Ticket><br>
> <MessageText>Example 2</MessageText><br>
> <Phone>+4409878656787</Phone><br>
> <Date>123456123</Date><br>
> </InboundMessage><br>
> <InboundMessage><br>
> <Ticket>1280</Ticket><br>
> <MessageText>Example 3</MessageText><br>
> <Phone>+4409878656787</Phone><br>
> <Date>123456123</Date><br>
> </InboundMessage><br>
> </Request><br>
> <br>
> <br>
> <br>
> I can't figure out from the manuals and docs that i have read how to<br>
> iterate through all the instances of <InboundMessage> so i can retrieve<br>
> the elements of each and bung them in a database.<br>
> <br>
> Would anyone have any ideas how to do so?<br>
> <br>
> thanks<br>
> Edd<br>
> <br>
> _______________________________________________<br>
> To control your jdom-interest membership:<br>
> http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost.com<br>
> <br>
<br>
-- <br>
wWw<br>
(o o)<br>
-------------ooO-(_)-Ooo-----------------------------------------------<br>
Laurent Bihanic | Tel: +33 (0)1 55.91.21.93 (direct)<br>
AtosOrigin Systems Integration | +33 (0)1 55.91.20.00<br>
Generic Solutions / SC | Fax: +33 (0)1 55.91.22.31<br>
Les Miroirs - Bat. C |<br>
18, avenue d'Alsace |<br>
F-92926 La Defense Cedex | E-mail: laurent.bihanic@atosorigin.com<br>
-----------------------------------------------------------------------<br>
<br>
Plonge tes racines dans la terre<br>
Laisse nous vivre avec le vent<br>
Passe l'hiver comme les graines<br>
Et chante au printemps comme les oiseaux<br>
<br>
<br>
DISCLAIMER:<br>
The opinions expressed are entirely my own and may not necessarily be<br>
those of my employer. Also, I am not now nor have I ever been a<br>
lawyer. My opinions are provided as-is with absolutely no warrantee of<br>
merchantability or fitness for any particular use. Besides, you can't<br>
prove I typed this. No body saw me type this. Who says I typed this?<br>
_______________________________________________<br>
To control your jdom-interest membership:<br>
http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost.com</font>
<br>
<br>
<br>