Persisting With XML, Or Else
By: David Utter
There are a few ways of persisting XML to a file, each with their pros and cons, that programmers may want to consider.
Brett D. McLaughlin, Sr., of O’Reilly Media, penned a post on XML persistence and ways to accomplish it. “You can do all sorts of interesting things with XML, but if you can’t persist it to a file, it’s all for naught,” he wrote.
Web services and other programming tasks may demand XML store its data someplace. Writing to a file may not be interesting or sexy, but it is necessary. McLaughlin listed three “pretty common mainstream” ways programmers likely accomplish this today:
1. Using the DOM and JDOM APIs and the like directly to write to a file from your XML data structure
2. Using the Transformation API for XML (TrAX) and the identity transformation to persist your XML
3. Using a higher-level API like JAXB to handle persistence
Using the APIs directly gives the programmer control over the output file in a broad way. That can be dangerous, McLaughlin noted, as “you have the ability to really mess things up by misconfiguring that output.”
TrAX, the Transformation API for XML, may be easier for newer programmers to use, and it’s included with all the Java platforms outside of Micro Edition. McLaughlin sees some peril in handling the output, and thinks handling it with DOM or JDOM directly would make it easier to work with that.
Data binding through the JAXB API isn’t a typical persistence method, but it is one McLaughlin suggests as an effective one for virtually everyone using Java. “You can work with normal Java objects - not XML-specific ones like DOM’s Node or Text interfaces - and take those objects straight to XML,” he noted.
Choosing a method comes down to what works best for a programmer, and for the application. If one method produces output that can be used effectively by a business, it’s probably the one that merits a further look.


