Java Architecture for XML Binding

from Wikipedia, the free encyclopedia
Java Architecture for XML Binding (JAXB)
Basic data

Maintainer Metro project
developer Sun Microsystems
Current  version 2.3.1
(September 12, 2018)
operating system Platform independent
programming language Java
category XML API
License CDDL v1.0 & GPL v2
jaxb-ri

Java Architecture for XML Binding , JAXB for short , is a program interface in Java that enables data from an XML schema instance to be automatically bound to Java classes and these Java classes to be generated from an XML schema . This process is called XML data binding .

Working with XML documents is thus possible without the developer having to use interfaces for processing XML such as SAX or DOM directly .

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

Since version 9 JAXB is no longer part of the JRE and the JDK.

Object-to-XML transformation

Marshalling and Unmarshalling

Marshalling generated when JAXB from a tree of Java objects an XML document. This is a special form of serialization . Use cases for this conversion are, for example, persistence in a file or transmission over a network. The opposite way is called unmarshalling - a special form of deserialization . A tree of Java objects is generated from a given XML document. XML schemas are used to uniquely implement such a conversion. The XML documents used obey the rules defined in the schema. They are also called schema instances.

Data binding

The term data binding within this concept describes a set of rules that determine the mapping / representation of the XML schema in relation to the Java objects to be generated. These predefined rules can be influenced by binding customizations . This can be done using inline notes in the XML schema or using a separate file that is then transferred to the binding compiler . This then has the task of mapping the XML schema to a corresponding Java object structure.

The table below lists the mapping of the XML schema data types (XSD) to the corresponding data types in Java in JAXB.

XML schema type Java data type
xsd:anySimpleType java.lang.Object
xsd:anySimpleType java.lang.String
xsd:base64Binary byte[]
xsd:boolean boolean
xsd:byte byte
xsd:date javax.xml.datatype.XMLGregorianCalendar
xsd:dateTime javax.xml.datatype.XMLGregorianCalendar
xsd:decimal java.math.BigDecimal
xsd:double double
xsd:duration javax.xml.datatype.Duration
xsd:float float
xsd:g javax.xml.datatype.XMLGregorianCalendar
xsd:hexBinary byte[]
xsd:int int
xsd:integer java.math.BigInteger
xsd:long long
xsd:NOTATION javax.xml.namespace.QName
xsd:positiveInteger java.math.BigInteger
xsd:QName javax.xml.namespace.QName
xsd:short short
xsd:string java.lang.String
xsd:time javax.xml.datatype.XMLGregorianCalendar
xsd:unsignedByte short
xsd:unsignedInt long
xsd:unsignedLong java.math.BigDecimal
xsd:unsignedShort int

JAXB binding framework

The JAXB Binding Framework essentially consists of three Java packages:

  • javax.xml.bind
  • javax.xml.bind.util
  • javax.xml.bind.helpers

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

The class " JAXBContext" from " javax.xml.bind" represents the entry point for using the framework within your 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"));

An "Unmarshaller" is now generated from the context object created. This can then be used to map the XML data to Java objects.

Since JAXB 2.1 the auxiliary class " JAXB" from " javax.xml.bind" represents a simplified way to achieve this. The creation of the context and the unmarshaller is automatically carried out internally by them, which makes using JAXB considerably easier, especially for beginners. The disadvantage here, however, is that these objects are regenerated each time they are called, so that a speed disadvantage results in some applications:

    FooObject fooObj = JAXB.unmarshal(new File("foo.xml"), FooObject.class);
    BarObject barObj = JAXB.unmarshal(new File("bar.xml"), BarObject.class);

In order to bring the data objects back into the XML form, a “marshaler” is generated from the context object. The data object and a stream object (" java.io.OutputStream" or " java.io.Writer") are transferred to it.

    Marshaller m = jc.createMarshaller();
    m.marshal(fooObj, System.out);

JAXB 2.1 also facilitates programming at this point with the help class " JAXB", which the marshaler generates internally and thus leads to a shorter source code:

    JAXB.marshal(fooObj, System.out);

Alternatives

The following Java XML Binding Frameworks can be used as alternatives to JAXB:

Web links

Individual evidence

  1. JAXB Reference Implementation ( Memento of the original of July 11, 2007 in the Internet Archive ) Info: The archive link was automatically inserted and not yet checked. Please check the original and archive link according to the instructions and then remove this notice. @1@ 2Template: Webachiv / IABot / jaxb.dev.java.net
  2. Java SE 9 (JSR 379) Final Release Specification: APIs proposed for removal. Accessed May 14, 2019 .
  3. JEP 320: Remove the Java EE and CORBA Modules. Accessed May 14, 2019 .
  4. Using JAXB Data Binding: Standard Data Type Mapping . Retrieved April 25, 2014.
  5. for xsd:elementthis type
  6. for xsd:attributethis type