[jdom-interest] Are Children under an Element returned in the proper order

Kesav Kumar kesavk at voquette.com
Thu Mar 8 09:40:12 PST 2001

You are absolutely correct.  This what happens when JDOM builds its internal
list reprasentation by getting the nodes info from the underlying parser
SAX/DOM.  Once the parse done its job JDOM has the same exact order what the
parser has given.  Things are fine as long as you have only nodes attributes
etc.  Once you have text content here and there in the document like the

This text belongs root
		This is child1
I am still under root
		This is child2

In this case things starts getting worse you will have two text elements in
your partiallist.  When you ask for the getText() method its ok it goes
through all the nodes and gives you the total string.  Things are wrose if
you start using the insertion with positions.  Long back I proposed to store
all the text content in one single list element but no one reponded for
that.  As long as XML doesn't have any mixed content all the places things
work as expected the order will be maintained as given by parser.  Once JDOM
has its list one can add at any position new elements.

Kesav Kumar
Software Engineer
Voquette, Inc.
650 356 3740
mailto:kesavk at voquette.com

-----Original Message-----
From: GB/DEV - Philip Nelson [mailto:philip.nelson at omniresources.com]
Sent: Thursday, March 08, 2001 9:12 AM
To: jdom-interest at jdom.org
Subject: RE: [jdom-interest] Are Children under an Element returned in
the proper order

> If JDOM did not maintain this order, then reading a
> document into JDOM and outputting it back to an XML
> string could take a valid document and make it
> invalid.
> I don't think it is an accident that the current JDOM
> preserves the order of elements, and hopefully this
> thread will be a reminder to any potential
> contributers that order can matter in an XML document,
> and so should be preserved by JDOM.

I'm definately wishing I had put silent mode on earlier ;-)
I haven't worked or tested this part of the api so those folks will have to
comment for a better answer than mine. 


When I made the statement, I was thinking of two things over which JDOM
would have a hard time controlling.  First, as someone else pointed out,
JDOM relies on the order the parser returns.  In the case of Sax, it will
always be the order of the original, but Sax parsers are separate from JDOM.
So the enforcment or order is not from JDOM.  However, you can insert child
elements yourself in the the list.  A JDOM Element can not tell the
difference between SaxBuilder inserting children or you inserting children,
and a JDOM Element will simply do what it is told.  Order is *absolutely*
preserved in Element.  The List implementation was chosen for that reason
and I have seen many comments from Jason and Brett instructing users to use
the List methods to insert elements in a specific ordinal position.


And now I will wait for an answer from someone who actually worked on
To control your jdom-interest membership:

More information about the jdom-interest mailing list