• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Format problem in XML

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
}
}
________________________________________________________________________________
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!