Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within XML
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
paul wheaton
Liutauras Vilda
Ron McLeod
Sheriffs:
Jeanne Boyarsky
Devaka Cooray
Paul Clapham
Saloon Keepers:
Scott Selikoff
Tim Holloway
Piet Souris
Mikalai Zaikin
Frits Walraven
Bartenders:
Stephan van Hulst
Carey Brown
Forum:
XML and Related Technologies
Wrong order of xml tags created while writing xml to a file
Akram Chotu
Ranch Hand
Posts: 43
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
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.
Srivatsa Katta
Ranch Hand
Posts: 55
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi,
Why is the order of attributes so much important for you?
What the code is generating and what you are expecting are the same.
In XML terminology they both are identical XML instances.
-Katta
Ulf Dittmer
Rancher
Posts: 43081
77
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
You're not missing anything. According to the XML specification, the order of attributes has no significance, so any XML processor is free to order them as it pleases.
Akram Chotu
Ranch Hand
Posts: 43
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Thank you.
Been there. Done that. Went back for more. But this time, I took this tiny ad with me:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
extracting image binary data from file
extracting image binary data from file
Can get the Form Feed "\f" to work!
SAX Parser
DDML
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
More...