Exploring JDBC and XML - Processing XML with Java and XOM
(Page 4 of 4 )
Processing XML with Java
Java 2 supports XML through the Java API for XML Processing, a set of packages for reading, writing, and manipulating XML data.
The javax.xml.parsers package is the entry point to the other packages. These classes can be used to parse and validate XML data using two techniques: the Simple API for XML (SAX) and the Document Object Model (DOM). However, they can be difficult to implement, which has inspired other groups to offer their own class libraries to work with XML.
You'll spend the remainder of the day working with one of these alternatives: the XML Object Model (XOM) library, an open-source Java class library that makes it extremely easy to read, write, and transform XML data.
Note - To find out more about the Java API for XML Processing, visit the company's Java Web site at http://java.sun.com/xml.
Processing XML with XOM
One of the most important skills you can develop as a Java programmer is the ability to find suitable packages and classes that can be employed in your own projects. For obvious reasons, reusing a well-designed class library is easier than developing one on your own.
Although the Java 2 class library from Sun contains thousands of well-designed classes that cover a comprehensive range of development needs, the company isn't the only supplier of packages that may prove useful to your efforts.
Dozens of Java packages are offered by other companies, groups, and individuals under a variety of commercial and open-source licenses. Some of the most notable come from Apache Jakarta, a Java development project of the Apache Software Foundation that has produced the Web application framework Struts, the Log4J logging class library, and many other popular libraries.
Another terrific open-source Java class library is the XOM library, a tree-based package for XML processing that strives to be simple to learn, simple to use, and uncompromising in its adherence to well-formed XML.
The library was developed by the programmer and author Elliotte Rusty Harold based on his experience with Sun's XML processing packages and other efforts to handle XML in Java.
The project was originally envisioned as a fork of JDOM, a popular tree-based model for representing an XML document that was first released in 2000. Harold has contributed code to that open-source project and participated in its development.
Instead of forking the JDOM code, Harold decided to start from scratch and adopt some of its core design principles in XOM.
The library embodies the following principles:
XML documents are modeled as a tree with Java classes representing nodes on the tree such as elements, comments, processing instructions, and document type declarations. A programmer can add and remove nodes to manipulate the document in memory, a simple approach that can be implemented gracefully in Java.
All XML data produced by XOM is well-formed and has a well-formed namespace.
Each element of an XML document is represented as a class with constructor methods.
Object serialization is not supported. Instead, programmers are encouraged to use XML as the format for serialized data, enabling it to be readily exchanged with any software that reads XML regardless of the programming language in which it was developed.
The library relies on another XML parser to read XML documents and fill trees instead of doing this low-level work directly. XOM uses a SAX parser that must be downloaded and installed separately. Right now, the preferred parser is Apache Xerces 2.6.1.
XOM is available for download from the Web address http://www.cafeconleche.org/ XOM. The most current version at this writing is 1.0d24.
Xerces can be downloaded from the Web at http://xml.apache.org/xerces2-j.
Caution - XOM is released according to the terms of the open source GNU Lesser General Public License (LGPL). The license grants permission to distribute the library without modification with Java programs that use it.
You also can make changes to the XOM class library as long as you offer them under the LGPL. The full license is published online at http://www.cafeconleche.org/XOM/license.xhtml.
After you have downloaded XOM and Xerces and added their packages to your system's Classpath, you're ready to begin using XOM.
The full installation instructions are available from the XOM and Xerces Web sites. The classes are distributed as JAR archive files—XOM in a file called xom-1.0d24.jar and Xerces in the files xercesImpl.jar, xercesParserAPIs.jar, and xercesSamples.jar. These four files should be added to your system's Classpath environment variable so that your Java programs can use XOM classes.
Please check back next week for the conclusion of this article.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
This article is excerpted from chapter 20 of the book Sams Teach Yourself Java 2 in 21 Days, 4th Edition, written by Rogers Cadenhead and Laura Lemay (Sams; ISBN: 0672326280). Check it out today at your favorite bookstore. Buy this book now.
|
|