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.

