File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes How to order element attributes while creating xml in java using xerces parser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "How to order element attributes while creating xml in java using xerces parser" Watch "How to order element attributes while creating xml in java using xerces parser" New topic
Author

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

Vinaya Bhaskar
Greenhorn

Joined: Feb 17, 2010
Posts: 7
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

Joined: Jul 20, 2005
Posts: 10201
    
166

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.

[My Blog] [JavaRanch Journal]
Vinaya Bhaskar
Greenhorn

Joined: Feb 17, 2010
Posts: 7
Dont mind! Please guide me the way. As per project we need it.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10201
    
166

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

Joined: Mar 22, 2000
Posts: 12803
    
    5
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to order element attributes while creating xml in java using xerces parser