Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

really no way to pretty print XML???

843834Jun 7 2004 — edited Jul 1 2004
I wonder if there is really no way to pretty print a DOM, using Java's standard API (http://java.sun.com/j2se/1.4.2/docs/api/index.html).
What I've tried is (assume having a org.w3c.dom.Document instance doc:
TransformerFactory tf = TransformerFactory.newInstance(); 
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty("encoding", encoding); 
transformer.setOutputProperty(OutputKeys.INDENT,"yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "3" );
DOMSource source = new DOMSource(doc);
FileOutputStream os = new FileOutputStream(file);
StreamResult result = new StreamResult(os);
transformer.transform(source,result);
which gives, e.g. something like
<?xml version="1.0" encoding="ISO-8859-1"?>
<documentData>
<Asset assetID="229609"/>
   <Picturedescription>
<Asset assetID="229609"/>
</Picturedescription>
</documentData>
which is not even proper indent. I can't believe that there is no way in pretty prenting an XML document, using Java's Standard API....
Does anybody know more about it?
Urs
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 29 2004
Added on Jun 7 2004
7 comments
258 views