This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to order element attributes while creating xml in java using xerces parser

 
Vinaya Bhaskar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trees is a element and i have many attributes in it. Please guide me to hoe can we order the attributes of element while creating XMl from java

<TreeCases>

<Trees Date="2010-07-28" Duration="0:2" End_Time="13:9:21"
Failed="6" Passed="28" Percentage="82%" Start_Time="13:07:45"
Total="34">


</TreeCases>
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you want to order them?

P.S: This is similar to many other topics in the forum where developers want to change the look of the xml contents even if it won't make any semantic difference.
 
Vinaya Bhaskar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dont mind! Please guide me the way. As per project we need it.
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinaya Bhaskar wrote: As per project we need it.


I wouldn't mind telling you how it has to be done, if there was a way to do it. There's nothing in XML schema which specifies the order of attributes in an element. I think you should ask the person, in your project, who laid down this requirement, why he/she thinks the order of attributes is important. Maybe he/she had something else in mind? Also, just out of curiosity, how exactly do you want to order them? Alphabetically?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Valid XML can have attributes in any order as has already been stated - so there are NO XML tools to do this directly.

You can write your own serializer to enforce whatever you want, the easiest would be based on SAX parsing.

Look at the interface org.xml.sax.ContentHandler - and org.xml.sax.helpers.DefaultHandler class

You can write a custom class to handle all the SAX parsing events, writing the event contents to a new file however you want.

Note that startElement gets an Attributes list - you can pull all of the attributes out of this list, order them however you want and write the revised Element tag to your new file.

When done you will have a good introduction to SAX parsing.

Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic