Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to order the attributes in an DOM Element?

user10672901Dec 29 2010 — edited Dec 29 2010
Hi,
I have written a java program to create an XML using DOM.

Now I want to add elements to the XML document with some attributes. But the order of the attributes in XML file is not the same as I added. So How can I set the order for the attributes.
Is there any API available?? Kindly help.

My Code is like this:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
doc = builder.newDocument();
Element rootTag = doc.createElement("Root");
doc.appendChild(rootTag);
Element childTag = doc.createElement("Child");
childTag.setAttribute("name","value1");
childTag.setAttribute("description","value2");
rootTag.appendChild(childTag);

The expected Output:
<Root>
<Child name="value1" description="value2"/>
</Root>


The Actual Result:

<Root>
<Child description="value2" name="value1"/>
</Root>

Regards,
Purush
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 26 2011
Added on Dec 29 2010
1 comment
610 views