By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,470 Members | 1,907 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,470 IT Pros & Developers. It's quick & easy.

really no way to pretty print XML???

P: n/a
Hi all

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,"y es");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,resu lt);

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
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
In article <55**************************@posting.google.com >,
um*@e-nvention.com (Urs Muntwyler) wrote:
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


You could do it yourself by just walking the tree, or reading it through
a SAX parser.

a.f.
Jul 17 '05 #2

P: n/a
Yes there is ...

JDOM has an XMLOutputter class and a Format class which are
excellent for XML pretty printing.

In addition there are utilities to convert to DOM from JDOM
and visa versa.
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.