ITWissen.info - Tech know how online

Java architecture for XML binding (JAXB)

JAXB stands for JavaArchitecture for XMLBinding and designates anapplication programming interface(API) for interaction between applications programmed in Java and XML documents. JAXB is an alternative to well-known technologies such as Simple API for

XML

(SAX) and Document Object Model

(DOM), as it offers more convenient options for writing and reading XML documents directly from the Java application. Inthis context, it is also referred to as "mapping

", and in this case means the direct mapping of object states to XML documents. Theframework - the class library - offers methods for the targeted manipulation of Java objects and XML documents. In contrast to the functionality of a special XML processor with subsequent transfer of XML elements to objects or the manual mapping of Java objects to XML documents, the API automates the transformationprocess. The transformation rules are defined by annotations or a special schema file. Annotations determine the elements to be mapped and vice versa a program is generated by the schema file, which independently generates the JavaBeans representing the XML documents. JAXB 2.1 is part of the Java Development Kit (JDK) from version 6.

Relationship between XML and Java

Relationship between XML and Java

The figure shows first of all on the right side the connection between XML documents and the Java API represented by JAXB. The mutual conversion of the data structures is clearly visible, for which the corresponding Java code of the developer is responsible. In the left part of the figure, the relationship between the XML schema and the Java classes of the API is clear. Here, the schema compiler and schema generator act to transform the data structure description and vice versa. This also shows the four basic functionalities of JAXB - serialization

, deserialization, schema generator, and schema compiler - which can be used entirely independently of each other.The JAXB API is a composite of several packages

:

  • javax.xml.bind - provides the general core functions such as serialization, deserialization, and validation.
  • javax.sml.bind.annotation - defines all annotations valid in JAXB.
  • javax.sml.bind.helpers - summarizes some standard implementations of the JAXB interfaces.
  • javax.xml.bind.util - defines JAXB service classes
To understand the JAXB API, the following concepts are assumed

:

  • Mapping a data structure to a sequential representation form - that is, writing data to an XML document - is called serialization or marshalling.
  • If a Java object is created from an XML document - that is, an XML document is read - this is called deserialization or unmarshalling.
  • Data binding is the process that leads to a mapping of data (for example, XML documents) to corresponding objects in memory.

Object creation

.to create objects from existing XML documents, JAXB provides the appropriate classes

and interfaces. Another prerequisite is the submission of an XML schema, which describes in XML syntax the relationships between the elements and attributes of the XML document. Theinput of the JAXB Binding Compiler is the XML schema. From this, the code for the JAXB classes is generated, which are then compiled together with the files of the JAXB bundle and the application. Provided that an XML document has been written according to the rules of the XML schema, the JAXB Binding Framework uses unmarshalling to transform it into a tree structure

that then maps the contents of the XML document. The structure tree consists of a set of Java objects that correspond to the instantiated JAXB classes. The structure tree can now be modified by the application. Once this process is complete, the structure tree can be transformed back into an XML document using marshalling. Both marshalling and unmarshalling offer the possibility to validate the data against the XML schema

The Goal of JAXB

The goal of JAXB is to support processing related to XML documents - directly from the Java application. To do this, a developer does not need any knowledge of technologies such as Simple API for XML (SAX) or Document Object Model (DOM), but simply uses the methods provided by JAXB. However, JAXB currently only supports the XML schema for defining the structure of XML documents. This was different in a first version - there the description of the structure in a Document Type Definition (DTD) was assumed. Due to the limitations of the DTD with regard to data types and their values or its poor handling of all formats that do not correspond to text formats, the requirements for validated and consistent XML documents could not be met, so that the XML schema is now used. JAXB is used, among other things, for reading in configuration files, generating and manipulating XML documents, validating user input, e.g. in a browser, and in connection with configuration management

forgenerating and maintaining configuration files.

https://www.jaxb.dev.java.net/

Informationen zum Artikel
Englisch: Java architecture for XML binding - JAXB
Updated at: 09.11.2013
#Words: 474
Links: