Join David Gassner for an in-depth discussion in this video Formatting documents with StAX utility classes, part of XML Integration with Java.
In the previous movie, I described how to fill the XML document with data, using StAX. And I showed you the beginning of the solution but not the end. In this project, StAXFormatXML. I have the completed code, in the class StAX stream creator. In addition to the ID, attribute and the customer NAME, I am now writing out the PHONE, ABOUT, AGE, BALANCE, ACTIVE, and JOINED values. All taking into account the various properties data types in my customer POJO class.
Right now, I'm writing out XML without formatting. So it's all compacted together. And just as with DOM or JDOM, if you're trying to create XML to send over the web, you might want to keep the XML compacted like this. But in many cases, you'll want to format it, adding indentation, creating an XML style known sometimes as pretty printing. There's a class available named indenting XML Stream Writer, and it's found in some StAX implementations, but not all.
And specifically it's not included in the Oracle JDK, version 7. So if you want to format your XML using the simplest possible approach, you'll need to go get a new JAR file. You can find this JAR file from this website at java.net/projects/staxstatsutils/downloads. To download the most recent stable version, as of the time of this recording, that was staxutils20070216.
So you can see that this is a very stable library, that has not been undergoing recent updates. I have already downloaded the ZIP file to my desktop and extracted it. And I'll find this JAR file, stax-utils and I'll copy that JAR file to the clipboard, then I'll go back to Eclipse to the package explorer and I'll paste that JAR file into the Lives folder. Then, I'll add it to the project's build path, by right-clicking on it and choosing Build Path > Add to Build Path.
If you're working in some other IDE other than Eclipse, follow that IDE's process for adding the JAR file to your build path. Next, I'll go to the class StAX Stream Creator. And this is where I'm writing out my XML. Currently, I'm writing it out to a string. Using the XML stream writer, and Java's string writer class. Now, because I have that JAR file in my build path, I can create an instance of the class IndentingXMLStreamWriter, which is a member of the package Javanet.staxsutils.
For the moment, I'm going to name this object writer, which will collide with the XMLStreamWriter above. But then, the goal is to create the IndentingXMLStreamWriter and wrap it around the XMLStreamWriter. So, I'm just going to rename this one as w. Don't use Eclipse's refactoring, you don't want to change all the references. And then, come back down here to where you're creating the IndentingXMLStreamWriter, and instantiate it with the class's constructor method.
Starting with new, and then the class, IndentingXMLStreamWriter and wrap it around the XMLStreamWriter. And that's it. The IndentingXMLStreamWriter class has a series of set methods, that you can use to control indentation levels, how new lines are written, and other features. But we'll use it with its default behavior. I'll save my changes. I'll come back to my main class and run. And there's the result.
The XML is output, and it's pretty printed with indentation. Now again the IndentingXMLStreamWriter class is included in some StAX distributions. Just not in the default distribution that's included with the Oracle JDK. But it's incredibly simple to use, and if you need formatted XML, it's the easiest way to get the job done.
- Choosing a Java-based XML API
- Reading XML as a string
- Comparing streaming and tree-based APIs
- Parsing XML with SAX
- Creating and reading XML with DOM
- Adding data to an XML document with JDOM
- Reading and writing XML with StAX
- Working with JAXB and annotated classes
- Comparing Simple XML Serialization to JAXB