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
> 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.
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.