Jakarta XML Binding
Java Architecture for XML Binding (JAXB) allows Java developers to map Java classes to XML representations. JAXB provides two main features: the ability to marshal Java objects into XML and the inverse, i.e. to unmarshal XML back into Java objects. In other words, JAXB allows storing and retrieving data in memory in any XML format, without the need to implement a specific set of XML loading and saving routines for the program's class structure. It is similar to xsd.exe
and XmlSerializer in the .NET Framework.
JAXB is particularly useful when the specification is complex and changing. In such a case, regularly changing the XML Schema definitions to keep them synchronised with the Java definitions can be time consuming and error prone.
JAXB is a part of the Java SE platform and one of the APIs in the Java EE platform, and is part of the Java Web Services Development Pack (JWSDP). It is also one of the foundations for WSIT. JAXB is part of SE version 1.6.
JAXB 1.0 was developed under the Java Community Process as JSR 31.[1] As of 2006[update], JAXB 2.0 is being developed under JSR 222.[2] Reference implementations for these specifications are available under the CDDL open source license at java.net.
Usage
The tool "xjc" can be used to convert XML Schema and other schema file types (as of Java 1.6, RELAX NG, XML DTD, and WSDL are supported experimentally) to class representations.[3] Classes are marked up using annotations from javax.xml.bind.annotation.* namespace, for example, @XmlRootElement and @XmlElement. XML list sequences are represented by attributes of type java.util.List. Marshallers and Unmarshallers are created through an instance of JAXBContext.
In addition, JAXB includes a "schemagen" tool which can essentially perform the inverse of "xjc", creating an XML Schema from a set of annotated classes.
Default data type bindings
The table below lists the mappings of XML Schema (XSD) data types to Java data types in JAXB.[4]
XML Schema Type | Java Data Type |
---|---|
xsd:string | java.lang.String |
xsd:positiveInteger | java.math.BigInteger |
xsd:int | int |
xsd:long | long |
xsd:short | short |
xsd:decimal | java.math.BigDecimal |
xsd:float | float |
xsd:double | double |
xsd:boolean | boolean |
xsd:byte | byte |
xsd:QName | javax.xml.namespace.QName |
xsd:dateTime | javax.xml.datatype.XMLGregorianCalendar |
xsd:base64Binary | byte[] |
xsd:hexBinary | byte[] |
xsd:unsignedInt | long |
xsd:unsignedShort | int |
xsd:unsignedByte | short |
xsd:unsignedLong | java.math.BigDecimal |
xsd:time | javax.xml.datatype.XMLGregorianCalendar |
xsd:date | javax.xml.datatype.XMLGregorianCalendar |
xsd:g | javax.xml.datatype.XMLGregorianCalendar |
xsd:anySimpleType (for xsd:element of this type) | java.lang.Object |
xsd:anySimpleType (for xsd:attribute of this type) | java.lang.String |
xsd:duration | javax.xml.datatype.Duration |
xsd:NOTATION | javax.xml.namespace.QName |
Versions
See also
- XML data binding
- JiBX – one of the fastest XML data binders [7][8]
- XMLBeans – a similar and complementary technology to JAXB from Apache Software Foundation
- TopLink – an object to relational and object to XML mapper from Oracle that supports JAXB 1.0
- EclipseLink MOXy – Open Source implementation of JAXB and object XML mapping services under the Eclipse Foundation
References
- ^ "JSRs: Java Specification Requests. JSR 31: XML Data Binding Specification". Retrieved 2011-02-15.
- ^ "JSRs: Java Specification Requests. JSR 222: Java Architecture for XML Binding (JAXB) 2.0". Retrieved 2011-02-15.
- ^ "xjc - Java Architecture for XML Binding". Retrieved 2011-02-15.
- ^ "Using JAXB Data Binding: Standard Data Type Mapping".
- ^ "Java SE7: Java XML Technology Enhancements". Retrieved 2012-03-21.
- ^ "Java SE 6: Features and Enhancements". Retrieved 2012-03-21.
- ^ Dennis Sosnoski. "XML and Java technologies: Data binding, Part 2: Performance". Retrieved 2011-02-15.
- ^ "XML Binding Frameworks in the Context of Service-Oriented Architecture". Retrieved 2011-02-15.
External links
- JAXB home page Reference Implementation on Project GlassFish
- previous JAXB home page
- A JAXB Tutorial by Wolfgang Laun
- JSR 222 (JAXB 2.0)
- JSR 31 (JAXB 1.0)
- The Java EE 5 Tutorial - Binding between XML Schema and Java Classes JAXB chapter of the Java EE 5 Tutorial
- JAXB Wizard