Primary Menu

Pretty-Print XML from a DOM

If you’re like me it can be useful to convert a DOM in Java into a formatted XML string. Pretty-printing a DOM Element can augment the information gleaned from your debugger when tracking down a bug, for example.

I’ll show you three ways to pretty-print XML from a Java DOM below. All of them format a Document but can be easily modified to serialize any Node. (Note: if you’re interested, you can download source code for this posting here.)

Code Listing 1: Using TrAX to pretty-print XML.

Using the TrAX API is straightforward (Code Listing 1), however dealing with whitespace in a DOM is rather tricky. It’s important to note that if your DOM was parsed from XML containing whitespace, this will by default be preserved in the object model. Furthermore, the default TrAX indentation engine (Xalan-Java) won’t reformat existing whitespace.

Code Listing 2: An XSLT stylesheet for pretty-printing.

Method 2: DOM Level 3 Load and Save

DOM Level 3 Load and Save (LS) is the new API for pretty-printing XML. Support for LS is included with JAXP 1.3 which is bundled as part of Sun’s JDK 1.5, but unfortunately pretty-print formatting only works in JDK 1.6 or better.

In my post I link to the documentation for XMLSerializer which clearly states that it has been deprecated since Xerces 2.9.0 (released in 2006) which is some time ago.

You’re right that JDK 1.5 has been EOL’ed, but if you’re like me you know that *many* businesses are still using it and that upgrading to 1.6 isn’t an option for them anytime soon. Method 3 is still relevant if you’re in this camp.

Although it’s deprecated, XMLSerializer works without any problems that I’ve observed.