Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

really no way to pretty print XML???

0 views
Skip to first unread message

Urs Muntwyler

unread,
Jun 8, 2004, 2:08:47 AM6/8/04
to
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,"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

Arthur Frane

unread,
Jun 15, 2004, 4:45:33 PM6/15/04
to
In article <55336143.04060...@posting.google.com>,
u...@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.

GIMME

unread,
Jun 16, 2004, 2:17:51 PM6/16/04
to
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.

0 new messages