SV: [jdom-interest] ordering elements within an XML document

Per Norrman pernorrman at
Mon Oct 27 15:52:39 PST 2003

quite simple, indeed ;-)

   String text = "Created " + new Date().toLocaleString();
   doc.getRootElement().getContent().add(0, new Comment(text));
   Element e = new Element("created").setText(new Date().toLocaleString());
   doc.getRootElement().getContent().add(0, e);

Or, perhaps even better:

   doc.getContent().add(0, new Comment(text));


> -----Ursprungligt meddelande-----
> Från: jdom-interest-admin at 
> [mailto:jdom-interest-admin at] För William Krick
> Skickat: den 27 oktober 2003 23:46
> Till: jdom-interest at
> Ämne: [jdom-interest] ordering elements within an XML document
> Hello all,
> I have what I hope is a simple problem with a simple solution.
> My application works with hierarchical data that is stored 
> internally within a database.  My application gives the user 
> the ability to export this data in XML format.  I would like 
> to add a tag to the created XML document that holds the 
> creation date/time expressed as a string.
> Unfortunately, if I take the existing document object (before 
> it is output), get the root element, and add a new tag, it 
> ends up at the end of the
> document.   For human readability reasons, I'd like the 
> date/time tag to be
> at the top of the document.
> After looking at the problem a bit, it seems that the only 
> option I have is to pull the Document object apart (getting a 
> list of child elements), create a new Document, add my 
> date/time tag, and then append all the child elements to it.  
> An ugly proposal at best.
> Is there some magic method that I'm missing that will solve 
> this problem for me?
> Should I be storing the date/time as an attribute of the root 
> element instead?
> ...
> Krick
> _______________________________________________
> To control your jdom-interest membership: 
uraddr at

More information about the jdom-interest mailing list