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

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


Hi,
quite simple, indeed ;-)

   String text = "Created " + new Date().toLocaleString();
   doc.getRootElement().getContent().add(0, new Comment(text));
or
   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));

/pmn

> -----Ursprungligt meddelande-----
> Från: jdom-interest-admin at jdom.org 
> [mailto:jdom-interest-admin at jdom.org] För William Krick
> Skickat: den 27 oktober 2003 23:46
> Till: jdom-interest at jdom.org
> Ä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: 
> http://lists.denveronline.net/mailman/options/jdom-interest/yo
uraddr at yourhost.com




More information about the jdom-interest mailing list