| Author |
Format problem in XML
|
Monoj Roy
Ranch Hand
Joined: Oct 10, 2007
Posts: 98
|
|
The below code generate a abc.xml file as follows <?xml version="1.0" encoding="UTF-8"?> <main version="3"> <remarks Att1="1" BB="2" ccc="2">My TestingMy Testing</remarks> </main> I need the following XML as I have added the attributes as follows em.setAttribute("Att1", "1"); em.setAttribute("ccc", "2"); em.setAttribute("BB", "2"); I dont want a alphabatical order <?xml version="1.0" encoding="UTF-8"?> <main version="3"> <remarks Att1="1" ccc="2" BB="2">My TestingMy Testing</remarks> </main> Can any body help me on the same ________________________________________________________________________________ package com.xml.readwrite; import java.io.FileOutputStream; import java.io.OutputStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Element; import javax.xml.transform.OutputKeys; public class Example { private static String Tag_Main = "main"; private static String Tag_Version = "version"; private static String Tag_SetupName = "setup-name"; private static String Tag_Remarks = "remarks"; public static void main(String[] args) throws Exception { OutputStream outStream = new FileOutputStream("C:/abc.xml"); DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory .newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory .newDocumentBuilder(); Document document = documentBuilder.newDocument(); Element rootElement = document.createElement(Tag_Main); rootElement.setAttribute(Tag_Version, "3"); document.appendChild(rootElement); Element em = document.createElement(Tag_SetupName); em.appendChild(document.createTextNode("Setup 1")); em = document.createElement(Tag_Remarks); em.appendChild(document.createTextNode("My Testing")); em.setAttribute("Att1", "1"); em.setAttribute("ccc", "2"); em.setAttribute("BB", "2"); em.appendChild(document.createTextNode("My Testing")); rootElement.appendChild(em); /* * TransformerFactory transformerFactory = * TransformerFactory.newInstance(); Transformer transformer = * transformerFactory.newTransformer(); DOMSource source = new * DOMSource(document); StreamResult result = new * StreamResult(outStream); transformer.transform(source, result); */ TransformerFactory transformerFactory = TransformerFactory .newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty("indent", "yes"); transformer.setOutputProperty("method", "xml"); transformer.setOutputProperty("media-type", "text/xml"); transformer.setOutputProperty("version", "1.0"); DOMSource source = new DOMSource(document); StreamResult result = new StreamResult(outStream); transformer.transform(source, result); outStream.flush(); outStream.close(); } } ________________________________________________________________________________
|
 |
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12268
|
|
I dont want a alphabatical order
Sorry, XML does not allow you to specify an order of attributes in an Element. If you have code that depends on the order of attributes (which is unlikely) you are doing it wrong. You should be using the name of the attribute to retrieve the value. Bill
|
Java Resources at www.wbrogden.com
|
 |
 |
|
|
subject: Format problem in XML
|
|
|