Skip to main content

Formatting XML output of attributes and elements

2 replies [Last post]
Joined: 2006-03-14

How would I go about formatting an XML Document text output so that the elements are properly indented, and every attribute is displayed on it's own line. The reason I want to accomplish this is we want a readable XML format output of a Java object with many attributes which we want to be able to do diffs on when we create new versions, and right now all the XML element's attributes are displayed on one line.

Currently I'm using the javax.xml.transform.Transformer to transform a org.w3c.dom.Document as a javax.xml.transform.dom.DOMSource to a StreamResult. I'm currently able to specify that elements are indented using the OutputKeys.INDENT.

Is there any way for me to create some kind of Java output handler that I can inject and have it format the XML text? I don't think XSLT would work in this situation because the object graphs that are being serialized are to numerous and complex to easily define in an XSLT (especially just to format the XML text).

My XML output code looks like this (is there any way to do a noformat syntax to post XML and java code to these forums?):

The XML I'm outputting looks like this:

What I'd like the XML to look like is to have line breaks before each attribute and to indent them past the element, like:

Message was edited by: dougnukem

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2005-06-27

I have used XMLUnit in the past to diff xml documents - it compares at the DOM level, perhaps making your requirement for attribute per line irrelevant - I would definitely recommend looking at it.

Otherwise, surely a generic XSLT template can format any XML for you? I don't know enough XSLT to be sure.

Joined: 2006-03-14

I was able to write a pretty print version of my XML Document by walking the DOM Node recursively, I based this primarily off the class from the "Java and XML" 3rd Edition book by Brett McLaughlin, and Justin Edelson (

I modified the code a bit to handle the formatting of Element attributes the way I wanted, here's my

My new generated XML looks like this now, isn't it pretty :)

Here's the Serializer test class to try it on your own XML:

The only minor issue I have now is that if I reimport that XML document and write it out again it double indents my information because there are TEXT nodes associated with the pretty print format. Is there a way to ignore them when loading in an XML document, I tried setting setIgnoringElementContentWhitespace(true) in the DocumentBuilderFactory but that didn't help:

// Get DOM Parser Factory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance( );

// Turn on validation, and turn off namespaces

DocumentBuilder builder = factory.newDocumentBuilder( );
Document doc = builder.parse(inputXML);