Java Architecture for XML Binding

Java Architecture for XML Binding, short JAXB, is a program interface in Java, which makes it possible to bind data from an XML Schema instance out automatically to Java classes, and to generate these Java classes from an XML schema out. This process is called XML data binding.

Thus, a work with XML documents is possible, without the programmer interfaces for processing XML like SAX or DOM must use directly.

JAXB 2.0 is part of the Java Platform, Enterprise Edition 5.0, Standard Edition 6.0. It is a part of the Web Services Interoperability Technology ( WSIT ). JAXB 1.0 was developed by the Java Community Process as JSR 31, JSR 222 The JAXB 2.0 as a reference implementation of JAXB is part of the Metro project, the GlassFish community.

Object -to- XML - transformation

Marshalling JAXB generated at from a tree of Java objects an XML document. This is a special form of serialization. Applications for this conversion, for example, persistence in a file or sent over a network. The reverse path is called unmarshalling - a special form of deserialization. Here, a tree of Java objects is generated from a given XML document again. To realize such a conversion uniquely, using XML schema. The XML documents used obey the rules defined in the schema. It is also called schema instances.

Data - binding

The term of the bond within this concept describes a set of rules that determine the image / representation of the XML schema against the to be generated Java objects. In this predefined rules can be taken by Binding Customizations influence. This can be done by inline notes in the XML schema or a separate file, which is then passed to the binding compiler. This then has the task to map the XML schema to a Java object structure.

JAXB Binding Framework

The JAXB Binding Framework consists of a core of three Java packages:

  • Javax.xml.bind
  • Javax.xml.bind.util
  • Javax.xml.bind.helpers

The last two packages provide support functions for the main package " javax.xml.bind ".

The class " JAXBContext " from " javax.xml.bind " represents the entry point for the use of the framework; within its own Java application

JAXBContext jc = JAXBContext.newInstance ( " com.acme.foo: com.acme.bar ");      Unmarshaller u = jc.createUnmarshaller ();      FooObject fooObj = ( FooObject ) u.unmarshal ( new File ( " foo.xml "));      BarObject barObj = ( BarObject ) u.unmarshal ( new File ( " bar.xml ")); From the created context object a " unmarshaller " is now generated. With this then the XML data can be mapped to Java objects.

Since JAXB 2.1 provides the helper class " JAXB " from " javax.xml.bind " represents a simplified way to achieve this. The generation of the context and the Unmarshallers is performed by automatically internally, which greatly facilitates just for beginners to use JAXB. The disadvantage here is that these objects are created anew with each call, so that in some applications results in a speed penalty:

FooObject fooObj JAXB.unmarshal = ( new File ( " foo.xml " ), FooObject.class );      BarObject barObj JAXB.unmarshal = ( new File ( " bar.xml " ), BarObject.class ); To bring the data objects returned in the XML form, a " marshaller " from the context object is created. He will pass the data object and a Stream object (" java.io.OutputStream " or " java.io.Writer ").

Marshaller m = jc.createMarshaller ();      m.marshal ( fooObj, System.out ); Also at this point facilitates JAXB 2.1, the programming by the auxiliary class " JAXB " which produces the marshaller internally and thus leads to shorter source code:

JAXB.marshal ( fooObj, System.out ); alternatives

The following Java XML Binding frameworks can be used as an alternative to that contained in the JRE JAXB:

  • Apache XMLBeans
  • Castor ( Framework)
  • JiBX
  • CookXML
  • XStream
  • Apache Commons Betwixt
  • Simple XML Serialization
432351
de