|
JDOM and TRaX
This week XML-Deviant takes a brief look at two emerging APIs -- JDOM and TRaX -- that have been the subject of recent discussion within the XML community.
|
|
Simplify XML programming with JDOM
JDOM is a unique Java toolkit for working with XML, engineered to enable rapid development of XML applications. Its design embraces the Java language from syntax to semantics. But is it better than existing -- and more standard -- XML APIs? Judge for yourself as we run through some examples and illuminate the design goals of this popular open-source project, which recently was formally accepted as a Java Specification Request.
|
|
JSR 102
JDOM 1.0
JDOM is a way to represent an XML document for easy and efficient reading, manipulation, and writing
|
|
Easy Java/XML integration with JDOM, Part 1
JDOM is a new API for reading, writing, and manipulating XML from within Java code. In April, Jason Hunter and Brett McLaughlin publicly released the API in a beta form. Based on the premise that using XML should be intuitive, simple, and productive, the API is optimized specifically for the Java programmer. In this article, the first of two parts, Hunter and McLaughlin explain how to use JDOM to read XML from an existing source.
|
|
Easy Java/XML integration with JDOM, Part 2
JDOM is a new API for reading, writing, and manipulating XML from within Java code. In Part 1 of this series, Hunter and McLaughlin explained how to use JDOM to read XML from an existing source. In this final part, they focus on how you can use JDOM to create and mutate XML.
|
|
JDOM, The Java DOM
Using XML with Java is amazingly simple: All you need is a JDK, some free class libraries, a text editor and some data to process. The Document Object Model (DOM) is a popular, standardized way of manipulating XML data. Java developers might prefer JDOM in the future, a more Java-oriented API for reading and writing XML Documents.
|
|
Tip: Converting from DOM : When you need SAX or JDOM output from DOM
In this tip, you'll learn how to convert DOM structures to SAX and JDOM to allow communication with applications that do not use DOM. The code listings demonstrate how to convert from DOM to an output stream for use by SAX, and how to convert from DOM to JDOM.
|
|
Tip: Converting from SAX : Using SAX to communicate with apps that need DOM and JDOM inputs
Brett McLaughlin explains how to use SAX to communicate with applications that require DOM and JDOM inputs. It's a useful technique: With the flurry of XML APIs available, developers now have to be able to easily move from one to another, and then on to another. The sample code provides a concrete example of converting from SAX to JDOM.
|
|
Tip: Using JDOM and XSLT
In this tip, Brett McLaughlin tells how to avoid a common pitfall when working with XSLT and the JDOM API for XML developers working in Java. You'll learn how to take a JDOM document representation, transform it using the Apache Xalan processor, and obtain the resulting XML as another JDOM document. Transforming a document using XSLT is a common task, and JDOM makes the transformation go quite easily once you know how to avoid the missteps. The code demonstrates how to use JDOM with the new Apache Xalan 2 processor (for Java).
|
|
JDOM: XML Meets Java Meets Open Source
XML is an extensible framework for encapsulating data. Its widespread usage has depended to a large degree on how various programming languages could support reading, writing, and manipulating XML. Standards like DOM and SAX were developed early on and provide a language-independent API for XML manipulation.
|
|
JDOM Provides Fast, Easy Access to XML from Java
JDOM (Java Document Object Model) is a new technology that enables Java developers to read, change, and write XML (Extensible Mark-up Language) data much more easily than ever before. Created by Jason Hunter and Brett McLaughlin, JDOM has just been released under an open source license. The JDOM Project has a website dedicated to promoting the understanding and use of JDOM (http://jdom.org), and the site has joined the O'Reilly Network, according to O'Reilly officials.
|
|
What is JDOM?
JDOM is an open source, tree-based, pure Java API for parsing, creating, manipulating, and serializing XML documents. JDOM was invented by Brett McLaughlin and Jason Hunter in the Spring of 2000. I asked Jason how it happened, and here’s what he told me:
|
|
Creating XML Elements with JDOM
One of my favorite things about JDOM is that 90% of the time, it works exactly like I expect it to work. I don’t have to look at the documentation because my best guess is almost always right. This is only sometimes true for SAX and almost never true for DOM. For example, suppose you wanted to create the JDOM representation of this element:
|
|
Creating XML Documents with JDOM
Let’s begin with a simple JDOM program that creates this XML document:
|
|
Writing XML Documents with JDOM
Once you’ve created a document, you’re likely to want to serialize it to a network socket, a file, a string, or some other stream. JDOM’s org.jdom.output.XMLOutputter class does this in a standard way. You can create an XMLOutputter object with a no-args constructor and then write a document onto an OutputStream with its output() method. For example, this code fragment writes the Document object named doc onto System.out.
|
|
Document Type Declarations
Documents created with JDOM can have document type declarations and thus can be valid. JDOM does not offer a complete object model for DTDs. However, it does allow you to point at an existing DTD or add an internal DTD subset to your documents.
|
|
Namespaces
Suppose instead of the simple custom vocabulary I’ve been using so far, you wanted to use the standard MathML presentation vocabulary as shown in Example 14.4.
|
|
Reading XML Documents with JDOM
Naturally, JDOM can read existing XML documents from files, network sockets, strings, or anything else you can hook a stream or reader to. JDOM does not, however, include its own native parser. Instead it relies on any of a number of very fast, well-tested SAX2 parsers such as Xerces and Crimson.
|
|
Reading XML Documents with JDOM
Once you’ve parsed a document and formed a Document object, you’ll probably want to search it to select out those parts of it your program is interested in. In JDOM, most navigation takes place through the methods of the Element class
|
|
Talking to DOM Programs
JDOM is not an acronym. It does not stand for “Java Document Object Model”. JDOM is not directly compatible with DOM (which is an acronym). That is to say, a JDOM Element is not a DOM Element. The JDOM Element class does not implement the DOM Element interface. JDOM’s Element class has methods that the DOM Element interface does not have and vice versa.
|
|
Configuring SAXBuilder
When reading a file or stream through a SAX parser, you can set various properties on the parser including the ErrorHandler, DTDHandler, EntityResolver, and any custom features or properties that are supported by the underlying SAX XMLReader. SAXBuilder includes several methods that just delegate these configurations to the underlying XMLReader:
|
|
Java Integration
JDOM is the most Java-centric of all the major APIs for processing XML. The JDOM developers have given a lot of thought to exactly how the JDOM classes fit into common Java systems such as the Java Collections API, Remote Method Invocation (RMI), and the I/O framework. Unlike DOM, the behavior of JDOM objects is very well defined with respect to Java operations such as cloning and serializing.
|
|
What JDOM doesn’t do
There is more to JDOM than what you’ve seen in the brief tour in this chapter. The next chapter will cover the capabilities of the various JDOM classes like Element and Attribute in depth, as well as taking a deeper look at how these constructs are represented
|