|
|
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
Java Architecture for XML Binding
By: Hitesh Seth
Digg This!
Two foundation technologies, Java and XML, represent the marriage of portable code and data. A key ingredient of a successful marriage is compatibility. XML and a number of XML-based vocabularies are being used extensively as the standard data-exchange mechanism (and beyond) by both stand-alone Java 2, Standard Edition (J2SE) applications and server-based Java 2, Enterprise Edition (J2EE) enterprise applications. A basic requirement in a number of these programmatic scenarios is the mapping of XML data as Java objects and/or vice versa (converting Java objects into XML). Although there exists a core set of APIs for processing XML, particularly the Java API for XML Processing, which includes basic DOM/SAX-based XML parsing capabilities, object-oriented Java programmers have always needed to consume, manipulate, and generate XML in a more object-oriented fashion through a standardized API set and utilities. The requirement is similar to those presented in scenarios where relational data must be mixed with Java objects, which has resulted in a number of object-relational mapping mechanisms. Also highlighting the need for an efficient XML binding mechanism are the two extremely different mechanisms that SAX and DOM present for efficiently processing XML. Whereas SAX (Simple API for XML) represents a very efficient, albeit low-level, event-based XML processing engine, DOM (Document Object Model) provides a much easier generic in-memory, tree-based navigation structure. In a nutshell, choosing DOM over SAX may result in significant memory impact, and choosing SAX may present significant programmatic complexity.
JAXB JAXB is particularly useful when the processing process is aware of the various XML Schema structures (for known XML documents). Typical uses include manipulating XML-based configuration files, traversing large XML feeds without DOM-based memory overhead, validating user-input data, and so on. Typical usage of the JAXB framework requires that the developer take an existing XML Schema-based data definition and then, using a schema compiler utility, convert it to a set of Java classes that represent the schema. JAXB, now in its Public Draft 2 status, provides an automated document-type mapping for a subset of XML Schema-based definitions. The implementation can be used to map both simple and complex XML Schemas. Table 1 shows the primitive data-type mapping between XML data and the resulting Java data types.
Key Highlights
At the time of this writing, the JAXB specification was available in Public Draft 2 (released on Oct 4, 2002). A 1.0 beta implementation of JAXB is also available from http://java.sun.com/xml/jaxb. This public draft and 1.0 beta implementation have been used as the basis for this article. JAXB has changed considerably since it was first conceived, the primary change being that JAXB-based binding frameworks can now generate Java classes directly from XML Schemas, eliminating the need to create custom binding schema files. According to Sun's Web site, a final version of the JAXB reference implementation and specification is expected to be available by Q1/2003.
Using JAXB, Step by Step Executing the XML Java schema compiler (xjc) automatically creates a set of Java classes that represents the Message Schema. The JAXB implementation provides a predefined Ant task (xjc), which can be used to map XML Schemas into a corresponding set of Java classes (as shown in Ant build file [see Listing 2]; if you're new to Ant, it is one of the most popular and powerful XML-based next-generation application build toolsets). Ant can be used to compile the set of generated classes as well. Figure 3 shows a UML class diagram of the generated classes. The resulting set of Java classes can be used by an application to interpret the XML documents representing the Message Schema (ReadMessage.java, Listing 3) or to create them (CreateMessage.java, Listing 4). As is probably clear from the Java source code, the ReadMessage application parses an input XML document and generates a simple representation of the XML Message. On the other hand, the Create Message application creates an XML-based message from the dynamic values.
JAXB Customization - Binding Declarations Similar to our previous example, Ant and the xjc schema compiler can be used to create the Java classes from the schema. Figure 5 shows a UML class diagram of the basic interfaces generated through the process. Listing 6 shows how the generated schema classes can be used to perform computations on the input XML. Listing 7 shows a scenario where the Java application is manipulating the Java content tree, in this case adding a new element called "comment".
Conclusion However, the ease of use that has been highlighted by the specification and reference implementation still makes JAXB a significant benefit to developers using Java and XML technologies together. Overall, we can expect that the JAXB specification/implementation will mark the beginning of standardized, automated, round-trip Java-XML binding capabilities. As B2B interactions are automated and standardized, an efficient mechanism to generate and consume XML documents becomes more important than ever. With the emergence of Web services, this is likely to be further signified; developers will be receiving complex XML documents through SOAP-based Web services invocations. JAXB provides a much-needed Java-XML binding combination.
References 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
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||