Skip to main content

Creating xml document

1 reply [Last post]
vinica
Offline
Joined: 2007-09-13

Hello,
I use the org.w3c.dom package to create xml files.

Following is my piece of code:
----------------------------------------------------------
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;

public class Test {
public static void main(String[] args)
throws ParserConfigurationException,
TransformerConfigurationException,
TransformerException
{
// Create a DocumentBuilderFactory and a DocumentBuilder
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(false);
dbf.setCoalescing(false);
dbf.setExpandEntityReferences(true);
dbf.setIgnoringComments(true);
dbf.setIgnoringElementContentWhitespace(false);
DocumentBuilder db = dbf.newDocumentBuilder();
// Instead of parsing an XML document, however, just create an empty
// document that you can build up yourself.
Document document = db.newDocument();

// Now build a document tree using DOM methods
Element book = document.createElement("book"); // Create new element

book.setAttribute("id", "javanut4");
book.setAttribute("attrib","text");
book.setAttribute("case","test");

// Give it an attribute
document.appendChild(book); // Add to the document

// Now create a TransformerFactory and use it to create a Transformer
// object to transform our DOM document into a stream of XML text.
// No arguments to newTransformer() means no XSLT stylesheet
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();

// Create the Source and Result objects for the transformation
DOMSource source = new DOMSource(document); // DOM document
StreamResult result = new StreamResult(System.out); // to XML text

// Finally, do the transformation
transformer.transform(source, result);
}
}
------------------------------------------------------------------------------------------------------------------------------------------------

The above piece of code gives the following output in jre 1.4:
<?xml version="1.0" encoding="UTF-8"?>--------------- +Output1+

However the same piece of code gives the following output using jre 1.5:
<?xml version="1.0" encoding="UTF-8"?>-------------------- +Output2+

Please observe that the tags are sorted when i use jre 1.5

What should I do so that i get the same output as Output1 when I use jre 1.5?? (I do not want the tags sorted). The proper is in Element.setAttributes that sorts the incoming attributes.

Please let me know if I need to clarify.

Many thanks,
Vinica.

Message was edited by: vinica

Message was edited by: vinica

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
joehw
Offline
Joined: 2004-12-15

My guess is that this is due to the difference between the Crimson parser in jdk1.4 and Xerces in jdk1.5. The Xerces parser requires that the attributes be sorted in order to perform a binary search. I don't think there's a way to produce the same output as Output1 when using jdk1.5. But you could certainly use the jaxp1.3 jars to make Output1 the same as Output2, if that's ok with your application.