my dog learned polymorphism
The moose likes XML and Related Technologies and the fly likes Format problem in XML Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Reply Bookmark "Format problem in XML" Watch "Format problem in XML" New topic
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
    
    1
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
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Format problem in XML
 
Similar Threads
How to create XML file programmatically?
Append New content in previous XML generated using StreamResult
create a xml from getting values from html forms
XML content is not forming when running from server
How to export and import from data from syabse database using hibernate