|
|
YOUR FEEDBACK
SOA World Conference
Virtualization Conference $200 Savings Expire May 16, 2008... – Register Today! Did you read today's front page stories & breaking news?
SYS-CON.TV |
TODAY'S TOP SOA & WEBSERVICES LINKS Product Review
XMLBeans
By: Hitesh Seth
Digg This!
"Java and XML - portable code and portable data." Even though this saying has been around since Java developers began using XML, developers have always faced a general XML programming-related productivity problem: manipulating XML content is rather different from manipulating Java objects. This problem has been amplified with the emergence of XML Schema, the W3C standard type system for XML documents. While XML Schema provides a rich type system for XML documents, it isn't simple. Above all, the XML Schema type system differs significantly from the Java type system (e.g., the notion of simple types versus complex types, a rich set of predefined simple types, schema by restriction, etc.) Several alternatives have already surfaced to solve some of these problems. For example, we have the tree-based Document Object Model (DOM), and the lower-level, events-based SAX (Simple API for XML), implemented by Apache Xerces, the almost de facto standard Java parser implementation. For Java programmers searching for a more natural object-oriented mechanism, there's the Java API for XML Binding (JAXB), which was recently released in its 1.0 specification. JAXB (covered in XML- Journal, Vol. 4, issue 1) intends to create a Java-XML binding framework that provides Java programmers with more natural object-oriented access to the underlying XML data. JAXB also supports XML Schema-based type binding (with some exceptions). Recently, BEA released a beta of XMLBeans, an XML token stream-based technology that provides easy navigation of XML documents using a combination of interfaces - Java types (which have been mapped to the relevant XML Schema-based types) and native XML cursor- or XQuery-based access. The result is a technology that combines the benefits and convenience of manipulating XML data using a familiar Java type tree but doesn't sacrifice access to the underlying XML infoset.
Key Concepts 1. XML Cursor: The XMLBeans implementation provides a simple DOM-like tree navigation API to the underlying XML document. This is available even if XML Schema (xsd files) are not compiled. 2. JavaBeans: A set of XML Schema (xsd files), known as an XSD type system, is processed by a schema compiler to generate a set of Java classes and interfaces corresponding to the XML Schemas. A one-to-one relationship is maintained - one XSD type corresponds to a single Java type (or interface). All schema types are derived from the base class XmlObject. It's also useful to note that XMLBeans objects are serializable, so they can be transmitted over RMI boundaries. Similar to Java's inherent reflection capabilities, XMLBeans also provides capabilities to introspect the XSD Schema type through a set of classes. In the current beta implementation, the models available for schema compilation are a hosted compiler and the built-in support within BEA WebLogic Workshop. 3. XQuery: With Apache Xerces2, XMLBeans provides the capability to run XQuery-based queries to query XML for specific data. What differentiates XMLBeans from other Java binding technologies such as JAXB and Castor is the fact that XMLBeans supports two synchronized XML access models (see Figure 1) - one to the underlying XML content as well as one to the strongly typed Java classes. In addition, one of the primary objectives for XMLBeans (in its final release) is to provide 100 percent support of all features and capabilities in XML Schema (as specified by the W3C Recommendation).
Usage Scenario The next step is to generate a set of classes that represent the Order XSD type system. To do this, upload the Order.xsd file to BEA's hosted XMLBeans compiler on http://dev2dev.bea.com, which returns a JAR file (and source, if required) of the resulting interfaces/ classes. This JAR file can then be included in any project (EJB, Web, or even a stand-alone Java application) to process XML documents for the particular type. Listing 3 shows one such application - calculating the total order amount. The JavaBeans-based type bindings generated by XMLBeans and JAXB are quite similar by design, which indicates that XMLBeans can almost be an upgradeable option to existing JAXB developers. It also hints at a possible convergence (hopefully!) of the two initiatives. The fact that XMLBeans preserves the underlying XML content can be illustrated by invocation of xmlText() on the complete document object or a subelement such as lines[i]. You'll find that the call preserves the comments and the order of elements in the original document. This is particularly important in many application scenarios. For instance, if an XML-based configuration file is used, comments will typically be added to enhance readability. XMLBeans can be used to manipulate the content (read/write/update) but will also preserve the important information in it, such as comments. Another scenario in which preserving the original XML text is very important is an XML-based workflow application in which the content is significantly enhanced/processed as it follows the various activities in the application. Checking document validation is simply calling the validate method on the XmlObject (which is the super class of OrderDocument). So far we've seen a mechanism by which we're simply reading the XML content; the type system generated by XMLBeans also includes full support of modification of XML elements and tags using the generated classes. Listing 4 shows one such example, in which a business rule, "10% discount to all items", is applied to the original XML document. Running the sample program in Listing 4 will also illustrate that XMLBeans preserves the underlying XML content.
Support for XQuery
BEA WebLogic Workshop Note that while BEA WebLogic Workshop provides out-of-the-box support for XMLBeans-based schema type support, any IDE can be used to import the generated class files used in applications.
Availability According to BEA officials, BEA plans to continue the support for hosted schema compilation service, which compiles schemas into Java classes. BEA is further exploring standardization efforts and ways to make XMLBeans broadly available through channels such as open source. However, there has been no official word on that yet. Support is available for XMLBeans through dev2dev-based newsgroups.
Conclusion
Resources XML JOURNAL LATEST STORIES . . .
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK BREAKING XML NEWS
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||