Wrong order of xml tags created while writing xml to a file

I am trying to write xml to a file using below code:

...

...

DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

Document doc = builder.newDocument();

// Insert the root element node

Element element = doc.createElement("StateCollateralStandards");

doc.appendChild(element);

for (int t=0; t<statesCount; t++)

{

Element stateElement = doc.createElement("State");

element.appendChild(stateElement);

String stateName = request.getParameter(t+"*statename");

stateElement.setAttribute("State1", stateName);

String aircraft = request.getParameter(t+"*A");

stateElement.setAttribute("A", aircraft);

String mortgage = request.getParameter(t+"*M");

stateElement.setAttribute("M", mortgage);

String negotiable = request.getParameter(t+"*N");

stateElement.setAttribute("N", negotiable);

String other = request.getParameter(t+"*O");

stateElement.setAttribute("O", other);

String title = request.getParameter(t+"*T");

stateElement.setAttribute("T", title);

String ucc = request.getParameter(t+"*U");

stateElement.setAttribute("U", ucc);

String vessel = request.getParameter(t+"*V");

stateElement.setAttribute("V", vessel);

String loanlevel = request.getParameter(t+"*Z");

stateElement.setAttribute("Z", loanlevel);

}

// Prepare the DOM document for writing

Source source =new DOMSource(doc);

// Prepare the output file

File file =new File("c:\\state.collateral.standard4.xml");

Result result =new StreamResult(file);

// Write the DOM document to the file

Transformer xformer = TransformerFactory.newInstance().newTransformer();

xformer.setOutputProperty(OutputKeys.INDENT,"yes");

xformer.setOutputProperty(OutputKeys.METHOD,"xml");

xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,"yes");

xformer.setOutputProperty(OutputKeys.STANDALONE,"yes");

xformer.transform(source, result);

...

...

Using above code I am expecting below xml file to be generated:

><StateCollateralStandards>

<State State1="AL" A="90" M="60" N="10" O="60" T="50" U="30" V="140" Z="40" />

<State State1="AL" A="90" M="120" N="10" O="120" T="100" U="60" V="140" Z="40" />

</StateCollateralStandards>

but instead I am getting below xml file:

<StateCollateralStandards>

<State A="90" M="60" N="10" O="60" State1="AL" T="50" U="30" V="140" Z="40" />

<State A="90" M="120" N="10" O="120" State1="AL" T="100" U="60" V="140" Z="40" />

</StateCollateralStandards>

Can anyone please tell me what is that I am missing?

Thank you for your time and reply.

[4719 byte] By [AkramChotua] at [2007-11-27 8:37:28]
# 1

You are missing two things:

1) the elements are in the right sequence, but the attributes are in a different sequence.

2) The XML spec says that the order of attributes need not be preserved by any processing program.

So, even if your code worked the way you wanted it to work, there is no guarantee that the next release of something in the stream would not change the sequence.

Attributes are not order-specific. Write your code on that basis and all will be fine.

If the information needs to be ordered, use elements, not attributes.

Dave Patterson

d.pattersona at 2007-7-12 20:34:49 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2
That answers my question Dave. Thank you.
AkramChotua at 2007-7-12 20:34:49 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...