Not logged in | Login
Using The JAXB Wizard And Code Seeder Palette
What You Need to Do
The following sections provide instructions on how access and use data to generate java classes from an XSD or DTD inside the IDE.
The Java Architecture for XML Binding (JAXB) provides a fast and convenient way to bind XML schemas to Java representations, making it easy for Java developers to incorporate XML data and processing functions in Java applications. As part of this process, JAXB provides methods for constructing, unmarshalling, and marshalling XML content & data using Java language objects. This allows you to leverage the flexibility of platform-neutral XML data in Java applications without having to deal with or even know XML programming techniques. The result is highly portable XML data joined with highly portable Java code that can be used to create flexible, portable applications and Web services.
When using integration solutions, data in web services is received and sent through jax-ws or jax-rpc. By the time the data has been received, it is converted to java classes which makes it easy to access when contained in the message. But not all data enters the system through web services (such as java based integration applications, where data is read from JMS or from the file system) even though there's a similar need to be able to access the data through java classes. The JAXB Wizard enables you to generate java classes from an XSD or DTD inside the IDE. The Code-Seeder Palette's functionality allows you to generate template code in order to marshal, unmarshal, construct JAXB Objects to and from many sources. Sources can be JMS Message, String, or File.
This section covers the following topics:
The JAXB APIs and tools are shipped in the jaxb subdirectory of the Java WSDP. This directory contains sample applications, a JAXB binding compiler (xjc), and implementations of the runtime binding framework APIs contained in the javax.xml.bind package. For more information on binding between XML Schema and Java Classes, refer to the following tutorial: http://java.sun.com/javaee/5/docs/tutorial/doc/bnazf.html.
The following steps demonstrate ONE particular way to bind XML schemas and Java representations. Depending on your requirements, the binding process can be achieved in several different ways..
The JAXB wizard enables you to generate and compile JAXB classes for a schema, while not requiring a knowledge of the JAXB XML Binding Compiler (xjc) Tool. The wizard also helps in code completion for generated classes. JAXB code generation and compilation is made part of the project's compilation and build task. Once the JAXB Binding is created, the code seeder Palette actions will help you in using the JAXB generated classes in constructing, marshalling, and unmarshalling
The Code-Seeder Palette (JAXB) Wizard contains the following palette actions.
The Constructs action generates code to instantiate JAXB objects. This code generation is useful in automating the generation of a series of getter and setter methods, especially when the JAXB object contains a series of nested objects. The code that is generated will not only instantiate the JAXB Object, but will also populate all the nested objects with initial values. This instantiation and population of values and objects can be done by passing a sample XML file. The Sample XML file needs to conform to the XSD/Schema used to generate the JAXB class. When sample XML file is not provided, all the nested JAXB members objects are instantiated.
If the sample XML file is not provided, the code generated will not populate the default values (as shown below).
Figure 1-6 Unpopulated Method
The Marshal palette icon action generates template code to marshal a JAXB object. This provides the ability to convert a Java object tree back into XML data. There is no difference between marshalling a content tree that is created manually using the factory methods and marshalling a content tree that is the result of an unmarshal operation. Clients can marshal a Java content tree back to XML data to a java.io.OutputStream or a java.io.Writer. You must know the Class name of the object you are trying to marshal. You can search for the class using the Find... button. The template code can be generated to marshal a JAXB object to a String, OutputStream, File, Writer, or byte array.
The desired method will be generated (as shown below).
Figure 1-9 Marshal Generated Method
The Unmarshal palette icon action will generate template code to unmarshal a JAXB object. This allows for any global XML element declared in the schema to be unmarshalled as the root of an instance document. The JAXBContext object allows the merging of global elements across a set of schemas (listed in the contextPath). This means that a client application is able to unmarshal XML documents that are instances of any of the schemas listed in the contextPath. You must know the Class name of the object you are trying to unmarshal. Since each schema in the schema set can belong to distinct namespaces, the unification of schemas to an unmarshalling context should be namespace-independent. You can search for the class using the Find... button. The template code can be generated to unmarshal a JAXB object from a String, InputStream, File, Reader, or byte array.
The Unmarshal Method will be generated (as shown below).
Figure 1-12 Unmarshal Method