Recent articles:
Popular archives:
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'
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:
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 goals of the XML reader/writer class are as follows:
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:
Archived Discussions (Read only)