How to pretty print Java objects to XML

Introduction
In this article, I will show how to pretty-print XML from Java objects. Generally and loosely speaking, the process of converting an object into a sequence of bits so that it can be stored on a storage medium or transmitted across a network is referred to as serialization, marshalling, or deflating, and going the opposite direction is referred to as deserialization, unmarshalling, or inflating (correspondingly). In this article, I use the terms serialization, marshalling, and deflating interchangeably (likewise, I use the terms, deserialization, unmarshalling, and inflating interchangeably). The point is not to define terms precisely but to illustrate quick, effective, and alternative methods of pretty-printing XML from Java objects.

Motivation
Let’s provide a little motivation:

  • Why anyone would want to serialize objects using XML?
  • Why anyone would want to pretty-print XML?

As for the first question, XML has become one of the most widely used format for data exchange and transfer between different programming languages and software systems/components. As for the second question, the ability to understand XML and the data it encapsulates requires for the format to be easily read. The second question perhaps needs some more argument. One may wonder, “Can’t I just open the XML content (once it’s in file format) with Internet Explorer or FireFox and have these programs auto-format the content?” It is true, once you get the XML content into a file, then IE and FireFox will prettify the content (IE does a better looking job than FireFox in my opinion). But what if you want to use other tools without auto-XML formatting, such as Notepad, to view the XML content? Also, what if you are trying to view the XML content while debugging? From my own experience, serializing an object into XML format is not enough, the developer must go one step further and prettify the XML for readability.

Let the reader judge for himself if XML is better unformatted or formatted. Here’s a snippet of XML that has no formatting and is all in one line.

<person><firstName>Jason</firstName><lastName>Mraz</lastName></person>

Here’s another XML snippet with formatting (elements are one different lines, and indentation makes it clear which nodes are child nodes).

<person>
     <firstName>Jason</firstName>
     <lastName>Mraz</lastName>
</person>
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s