Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

XML JavaBeans, Part 2

Automatically convert JavaBeans to XML documents

  • Print
  • Feedback
A flood of mail from excited JavaWorld readers indicates that with my previous column I've hit a nerve: there's a great deal of interest in XML in the Java community. Readers are also particularly interested in the marriage of JavaBeans with XML, because it's a concrete example of using a platform-neutral component technology (JavaBeans) with a generalized document format (XML). The result of this combination is a component technology that is network-mobile, standards-based, and potentially interoperable with new and legacy systems alike.

I've received some great ideas from readers on how to improve XML JavaBeans, so I've expanded this topic from two installments to three. This month, we're going to develop a class, XMLBeanWriter, which writes a JavaBean in XML format. Then next month, we'll look at some of the improvements suggested by readers, and implement them (in Java, of course).

Last month's sample program, XMLBeanReader, reads XML and produces a running JavaBean in memory. If you're not yet up to speed on exactly what XML is, or if you're not familiar with the World Wide Web Consortium's (W3C's) Document Object Model (DOM), you'll probably want to read last month's cover story, " XML JavaBeans, Part 1." Once you've read and understood that article, you'll be up to speed for this month's topic, XMLBeanWriter.

Flattening object structures

In most nontrivial programs, information is processed using data structures. In object-oriented programming, these data structures are usually composed of objects that contain the data of interest to the application. It's often desirable to use the data in the application objects in more than one work session. (Just try to imagine a word processor without a Save command: turn off your computer, and all of your documents would die with the editor!) Persistence is the common term for how a software system arranges for such data to "survive" the death of the process in which they are running, so that they may live to run again another day (or on another machine).

Persistence has been around longer than object-oriented programming -- at least in practice, if not in name. Word processor files, data requests moving through a network, and punch card decks are all examples of persistence. In fact, data persistence is the whole reason for all of the various types of data media we have: from the ancient and lowly clay tablets and papyrus, to the slightly more modern paper tapes, magnetic reels, and punch cards of yesteryear, to today's CD-ROMs and DVDs. The basic idea behind persistence is to encode a software structure into a stream of bytes (a process sometimes called "flattening") in such a way that the stream can later be used to recreate the identical structure.

Persistent objects
When object-oriented system design and programming began to appear on the scene, it was immediately clear (to anyone who knew about such things) that it would be very convenient to be able to create persistent objects. An object running inside a program could be converted to a series of bytes, and then stored or transmitted, and that series of bytes could be used later and/or elsewhere to recreate the object. This is precisely what Java serialization does. The Java core has a set of built-in methods (in package java.io) that allows a programmer to arrange for an object to write itself to a byte stream, or to read itself from a byte stream. (To find out more about Java serialization, see the JavaWorld articles on those topics, linked in Resources below.) Persistence in the context of object-oriented systems is commonly called object persistence; or, said another way, objects that have been "persisted" (note that "persist" has suddenly become a transitive verb) are called "persistent objects".

  • Print
  • Feedback

Resources
  • Source code for this article is available for download in ZIP format at http://www.javaworld.com/jw-03-1999/beans/XMLBeansMarch99.zip
  • Get the source code in gzippped tar format at http://www.javaworld.com/jw-03-1999/beans/XMLBeansMarch99.tar.gz
  • You can also get a classfile-only jar file at http://www.javaworld.com/jw-03-1999/beans/XMLBeansMarch99.jar
  • The poem alluded to in the article is "Trees" by Joyce Kilmer, and can be found at http://www.venus.net/~nwashel2/poem.trees.html
  • The baseball player in our sample XML file is named Jonas Grumby. Extra credit for anyone who can tell me who that is without doing a Web search.
  • Previous columns on serialization mentioned above include:
  • "Do it the "Nescafé" way -- with freeze-dried JavaBeans" at http://www.javaworld.com/jw-01-1998/jw-01-beans.html
  • "Serialization and the JavaBeans Specification" at http://www.javaworld.com/jw-02-1998/jw-02-beans.html
  • "Serialization grab bag" at http://www.javaworld.com//jw-04-1998/jw-04-beans.html
  • You can read the complete XML standard, called Extensible Markup Language (XML) 1.0 W3C Recommendation, here http://www.w3.org/TR/1998/REC-xml-19980210
  • Read the RDF (Resource Definition Framework) standard -- a work in-progress -- at http://www.w3.org/RDF/
  • One of the better "one-stop shopping" sources for XML information is at XML.com. It has links to just about everything in the XML world. One of the more interesting things at this site is, believe it or not, the commentary on XML technology http://www.xml.com
  • A current version of the XML FAQ by Peter Flynn, et al. This is the version of the FAQ recommended by the W3C http://www.ucc.ie/xml/
  • The parser from IBM's xml4j package is available free for noncommercial use. It's even free for commercial use, but be sure to read the license agreement http://www.alphaWorks.ibm.com/formula/XML
  • In a note unrelated to JavaBeans, but still too cool for words, check out Jikes, IBM's new open source java compiler! Find out about it at the alphaWorks site at http://www.alphaWorks.ibm.com/formula/JikesOS
  • For IBM's Bean Markup Language (BML), see http://www.alphaWorks.ibm.com/formula/BML
  • If you're interested in the fine details of the current Document Object Model (Level 1) specification, you can find it at the W3C's Web site at http://www.w3.org/TR/REC-DOM-Level-1/level-one-core.html
  • Microsoft has a good set of tutorials on XML at http://www.microsoft.com/xml/tutorial/default.asp
  • There's also a whole XML "workshop" area. Don't try to access the workshop in Netscape, thoughthe table of contents doesn't work! These documents are free training, and are well written (though the examples don't always work, even in IE5beta.) Just don't be fooled into thinking that everything there is open standard. Some of the tutorials and many of the articles are about Microsoft-only technology that won't work with all browsers or platforms http://www.microsoft.com/xml/default.asp