Java Reference
In-Depth Information
One of the main advantages of using the DOM approach to handling XML documents is that once you
have parsed the document into a DOM object, you can access any element as required. This feature is
illustrated in the next section on using Xbeans to process XML documents.
Using Xbeans as Pluggable XML Processing Blocks
One of the most straightforward ways to work with XML documents in Java is to use Xbeans. Xbeans
are the brainchild of Bruce Martin, whose work lies at the core of Xbeans.org , an open-source project,
where you can read Bruce's excellent white paper "Creating Distributed Applications Using Xbeans."
You can also download the basic Xbean interface library from this site.
Essentially, Xbeans are pluggable XML-processing blocks. They are intended to be connected in chains,
where each Xbean performs one logical step in processing an XML document. The Xbean then passes
the document to the next Xbean in the chain as the event object in a bean event, as illustrated in Figure
17-2 .
Figure 17-2: Xbean connectivity
Connectivity is the primary key to the Xbean concept. Each Xbean in a chain performs one processing
step; then it passes the processed XML document to the next bean for further processing. This
approach makes it easy to break a project down into simple, frequently repeated operations, each of
which can be implemented as a reusable component.
Xbean connectivity is implemented using the delegation event model to communicate with other Xbeans.
The delegation event model is implemented using the DOMEvent interface, which defines a DOM
document as the event object. By firing a DOMEvent , the event source Xbean passes an XML
document as the DOMEvent object to the EventListener bean.
The Xbean connectivity model is defined by two interfaces included in the package org.Xbeans :
 
DOMSource defines two methods, setDOMListener() and getDOMListener() , for setting
and getting the Xbean that receives the output.
 
DOMListener defines a single method, documentReady(DOMEvent e) , which is called by the
event source Xbean to pass the XML document encapsulated in the DOMEvent .
Conventionally, the documentReady() method calls a processDocument(DOMEvent e) method,
which processes the XML document and returns it. If there is another Xbean in the chain, the processed
document is wrapped in a new DOMEvent and passed on to the next Xbean in the chain.
In practice, the simplest way to use Xbeans is to create a base class with an empty
processDocument() method. Then extend the Xbean base class as required, overriding the
processDocument() method to implement the desired functionality. Listing 17-2 illustrates the Xbean
base class.
Listing 17-2: XBean base class
package JavaDatabaseBible.ch17.Xbeans;
import org.Xbeans.*;
import org.w3c.dom.Document;
/**
Search WWH ::




Custom Search