Java: A platform for platforms
Sun's reorg may seem promising to shareholders but it's also a scramble for position. The question now is whether Sun can, or wants to, maintain its hold on Java technology. Especially with enterprise leaders like SpringSource and RedHat investing heavily in Java's future as a platform for platforms

Also see:

Discuss: Tim Bray on 'What Sun Should Do'

Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

XML DOM-lite parser and writer

Write and parse XML

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Applications frequently need to store persistent data, and the format for saving can be any format conceivable by the developer. However, each time a new application is written, the set of tools for saving and retrieving the information must be rewritten. Binary object files are simple to write in Java, but are not fully portable to other languages, nor are they human readable. Character-delimited files are highly portable but difficult to read and require encoding to embed the delimiter. As applications evolve, the complexity of the data to be managed can often grow more complex, and future generations of the application must migrate existing users to the new formats.

A simple and flexible data format is needed, with a set of methods for manipulation; the format should have the following characteristics:

  • Human readable for simplicity
  • Self descriptive so that data context is in the file
  • Extensible so that applications can grow without rewriting the file format


Fortunately, the format and the tools already exist in Java as well as most other computer languages in the form of XML. XML data is ideal for storing, saving, and retrieving persistent data; however, reading and writing XML is not so simple. J2SE provides a SAX (Simple API for XML) parser, and JDOM is a freely available DOM (Document Object Model) parser, but both introduce complexities. Incorporating SAX into an application is challenging, and no methods are available for creating the XML file, leaving the programmer to find a means for generating XML. JDOM provides both the writer and the parser, but requires either a 3-megabyte download, thus burdening the user with download and installation, or an application distribution that includes the full JDOM package.

For large applications, these complexities are little more than a challenge to the developer. But, for small applications that do not need the full XML standard, a small and simple XML class is needed. A quick Web search for a simple XML parser resulted in JavaWorld's "Java Tip 128: Create a Quick-and-Dirty XML Parser" by Steven Brandt, which provides a compact and small method for reading XML in SAX style. But incorporating this code results in two drawbacks: First, the approach requires the complexity of writing callback methods inherent to a SAX parser. Second, it does not provide a means for generating XML code to write valid XML.

With about a day of work, Brandt's SAX XML parser can be converted into a simple DOM-type parser that has the ability to manage data as well as read and write the data in XML format. The parser, which I introduce in this article, operates identically to Brandt's parser with the same limited scope of format.

The implementation

The goals of the XML reader/writer class are as follows:

  • It must be small and contained in a single class (including the exception classes)
  • It must maintain data in memory for simple manipulation with a set of methods similar to those of a vector
  • It must store each element as a separate object capable of recursion
  • Each object must independently implement attributes
  • It must provide the basic tags needed to store a simple configuration


XML element representation

Before beginning to convert Brandt's parser, the object representing an XML element must be created. Each XML object contains the tag, attributes, and the data of the element. The element's data may be a single value or a list of elements. The Java code for the element is shown below:

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources