+

Search Tips   |   Advanced Search

Use JAXB for XML data binding


Java Architecture for XML Binding (JAXB) is a Java technology that provides an easy and convenient way to map Java classes and XML schema for simplified Web services development. JAXB provides a schema compiler, schema generator and a runtime framework to support two-way mapping between Java objects and XML documents.

JAXB is an XML-to-Java binding technology that enables transformation between schema and Java objects and between XML instance documents and Java object instances. JAXB technology consists of a runtime API and accompanying tools that simplify access to XML documents. Use JAXB APIs and tools to establish mappings between Java classes and XML schema. An XML schema defines the data elements and structure of an XML document. JAXB technology provides tooling to enable you to convert the XML documents to and from Java objects.

Data stored in an XML document is accessible without the need to understand the XML data structure.

JAXB is the default data binding technology used by the Java API for XML Web Services (JAX-WS) tooling and implementation within this product. We can develop JAXB objects to use within the JAX-WS applications. We can also use JAXB independently of the JAX-WS model as a convenient way to leverage the XML data binding technology to manipulate XML within the Java applications.

Supported configurations: WAS V7.0 supports the JAXB 2.1 specification. JAX-WS 2.1 requires JAXB 2.1 for data binding. sptcfg

Supported configurations: The wsimport, wsgen, schemagen and xjc command-line tools are not supported on the z/OS platform. This functionality is provided by the assembly tools provided with WAS running on the z/OS platform. Read about these command-line tools for JAX-WS applications to learn more about these tools.

JAXB provides the xjc schema compiler tool, the schemagen schema generator tool, and a runtime framework. The xjc schema compiler tool enables you to start with an XML schema definition (XSD) to create a set of Java Beansthat map to the elements and types defined in the XSD schema. We can also start with a set of Java Beans and use the schemagen schema generator tool to create the XML schema. After using either the schema compiler or the schema generator command-line tools, we can convert the XML documents both to and from Java objects and use the resulting Java classes to assemble a Web services application.

In addition to using the tools from the command-line, we can invoke these JAXB tools from within the Ant build environments. Use the com.sun.tools.xjc.XJCTask Ant task from within the Ant build environment to invoke the xjc schema compiler tool. Use the com.sun.tools.jxc.SchemaGenTask Ant task from within the Ant build environment to invoke the schemagen schema generator tool.

JAXB annotated classes and artifacts contain all the information that the JAXB runtime API needs to process XML instance documents. The JAXB runtime API enables marshaling of JAXB objects to XML files and unmarshaling the XML document back to JAXB class instances. The JAXB binding package, javax.xml.bind, defines the abstract classes and interfaces that are used directly with content classes. In addition the package defines the marshal and unmarshal APIs.

JAXB 2.1 provides enhancements such as improved compilation support and support for the @XMLSeeAlso annotation. With JAXB 2.1, we can configure the xjc schema compiler so that it does not automatically generate new classes for a particular schema. Similarly, we can configure the schemagen schema generator to not automatically generate a new schema. This enhancement is useful when we are using a common schema and you do not want a new schema generated. JAXB 2.1 also introduces the @XMLSeeAlso annotation that enables JAXB to bind additional Java classes that it might not otherwise know about when binding a Java class with this annotation. This annotation enables JAXB to know about all classes that are potentially involved in marshalling or unmarshalling as it is not always possible or practical to list all of the subclasses of a given Java class. JAX-WS 2.1 also supports the use of the @XMLSeeAlso annotation on a service endpoint interface (SEI) or on a service implementation bean to ensure all of the classes referenced by the annotation are passed to JAXB for processing.

We can optionally use JAXB binding customizations to customize generated JAXB classes by overriding or extending the default JAXB bindings when the default bindings do not meet the business application needs. In most cases, the default binding rules are sufficient to generate a robust set of schema-derived classes. JAXB supports binding customizations and overrides to the default binding rules that we can make through various ways. For example, we can the overrides inline as annotations in a source schema, as declarations in an external bindings customization file used by the JAXB binding compiler, or as Java annotations within Java class files used by the JAXB schema generator. See the JAXB spec for information regarding binding customization options.

Use JAXB, we can manipulate data objects in the following ways:

 

 

Results

You now have JAXB objects that the Java application can use to manipulate XML data.


Use JAXB tools to generate an XML schema file from a Java class
Use JAXB tools to generate JAXB classes from an XML schema file
Use the JAXB runtime to marshal and unmarshal XML documents
xjc command for JAXB applications
schemagen command for JAXB applications

 

Related concepts


JAXB
Assembly tools

 

Related


Web services specifications and APIs

 

Related information


JAXB 2.0 Reference implementation
JAXB 2.0 spec documentation