JAXB parse XML conversion to Pojo__java

Source: Internet
Author: User
Tags format definition

One. Overview

JAXB (Java Architecture for XML Binding) is a standard XML schema and Java data Binding framework in the Java EE system, and developers can use JAXB to easily make XML data and Java The conversion of the object. JAXB provides a way to parse an XML document into a Java content tree and to write the Java content tree back to an XML document. JAXB also provides support for generating Java classes based on XML schemas and for generating XML schemas from Java classes.

Two. The JAXB framework is divided into the following three components:

Schema compiler: The schema compiler generates a Java class set in XML schema that corresponds to a schema, and the generated Java class automatically includes the corresponding JAXB annotations based on the schema structure.
For example, one element of the schema mapping Java classes are automatically annotated with @XmlType annotations, and the Java fields mapped by the attributes of an element are automatically annotated with @XmlElement annotations;

Schema Builder: Schema builder generates the corresponding XML schema for input with the Java class set that contains JAXB annotations. Let's say a Java class that contains @XmlType annotations generates a
The schema is mapped to an element;

JAXB Runtime Environment: The JAXB Runtime Environment provides two basic operations for access to XML documents, operations and validation, and so on: Marshal, Unmarshal, which are also the basis for XML and Java bindings. Marshal refers to the process of grouping a memory object as an XML document, and Unmarshal is the reverse process of parsing an XML document into a Memory object.


Three. JAXB Foundation---xml Schema
XML Schema is the format definition of XML document, and is a substitute product of DTD to constrain the content and structure of XML document, for example it defines the elements that XML document can appear.
The order in which elements appear, the attributes they have, and so on. The XML schema itself is an XML document, so it is inherently extensible in XML.
At the same time, XML schemas provide richer support for XML data types. The basis of the JAXB framework is the XML schema, a typical occasion for using JAXB is based on the business data model
The XML schema needs to be written first, and then the corresponding Java class set of XML is generated directly using the binding compiler of the JAXB framework.
Four. The relationship between JAXB and Web services

The Jax-ws Web Service completes the binding agent between XML and Java, meaning that the conversion between XML data and Java objects during Web service invocation is done by the JAXB framework. The data type descriptions in the Web service Interface reference parameters, return types, and so on are defined in the associated XML schema, and the JAXB framework is the XML schema associated with the Web service that completes the mapping between Java objects.


Mapping developers from Java to XML schemas can be done with the annotations provided by JAXB, which can be easily done using JAXB rich annotations when programming Java classes.
Five. customizing Web Services with JAXB
@XmlRootElement annotations are used to annotate a class or enumeration type, and the class that is annotated will appear as a global element in the mapped schema

Parameters: Name Custom mapped schema global element names, typically @XmlRootElement-annotated classes appear in the corresponding XML document in the outermost or root node form.

namespace defines this root node namespace, and the namespace name of the XML element
Cases:

@XmlAccessorType define what type of mapping this class needs to map to XML.
Four parameters can be received, respectively:

Xmlaccesstype.field: Maps Private non-static All fields in this class to XML

Xmlaccesstype.property: Mapping attributes in this class (Get/set method) to XML

Xmlaccesstype.public_member: Maps All public field or property in this class to XML at the same time (default)

Xmlaccesstype.none: Do not map
For example: @XmlAccessorType (Xmlaccesstype.field)
@XmlAccessorType (value = xmlaccesstype.property)

@XmlType
@XmlType annotations are used to annotate a class or enumeration type, and the class that is annotated will appear as an XML complex data type in the mapped schema

Parameters: Proporder Specifies the order of nodes in which XML is mapped to specify the order of output to customize the sequence of contents of the mapped complex data types

FACTORYCLASS Specifies the factory class required to generate a mapping class instance when Unmarshal is specified, which defaults to the class itself

FactoryMethod Factory method for the specified factory class

Name of the type in the XML schema, customizing the name of the mapped XML data type

Namespace specify the example of a namespace in a schema:

@XmlElement
1) @XmlElement annotation is used to annotate the attributes of the Javabean, and the attributes that are annotated with it appear as elements in the mapped schema, and the combination of all Javabean attribute mappings becomes
@XmlType a complex data type that is mapped.
2 Specifies that a field or Get/set method maps to a node of XML. For example, when the xmlaccessortype of a class is labeled as property, this annotation is annotated on a field that does not have a Get/set method.
You can map the field to XML.

Parameters: DefaultValue Specify node defaults

Name to specify the name of the node, and to customize the name of the XML element that is mapped

Namespace specify the node namespace

Required specifies whether the element must appear. if (default is False) required () is true, the JavaBean property is mapped to an XML schema element declaration minoccurs= "1" 。 MaxOccurs is "1" to represent a single assigned property, and MaxOccurs is "unbounded" to represent multiple assignment properties.

nillable Whether the field contains the nillable= "true" property (default is False)

Type defines the association types for this field or property
Cases:

@XmlAttribute Specify a field or Get/set method to map to an attribute of XML. Used to annotate the Javabean attribute, which is represented by attributes in the mapped schema as element attributes

Parameters: name Specifies the name of the property, and the name after the map is customized

Namespace Specify attribute namespaces

Required must (default is false to specify whether the mapped property is a required
Cases:
This Java class is represented in the mapped Web service schema:

@XmlTransient Defining a field or property does not need to be mapped to XML. For example, when the xmlaccessortype of a class is marked as property,
This annotation is annotated on a field of a Get/set method, and the property is not mapped.


Defines a parent node @XmlElementWrapper an array element or a collection element. For example, there is a list items in the class, and if this annotation is not added, the element is mapped to

<items>...</items>

<items>...</items>

This form, this annotation can be wrapped in this element, such as:

@XmlElementWrapper (name= "items")
@XmlElement (name= "item")
public List items;

This XML style will be generated:

<items>

<item>...</item>

<item>...</item>

</items>

@XmlJavaTypeAdapter a custom field or property that is mapped to an adapter that is XML. If the class contains an interface, we can define an adapter (inherited from the Javax.xml.bind.annotation.adapters.XmlAdapter Class) and specify how this interface maps to XML.

@XmlSchema Configure the namespace of the entire package, this annotation needs to be placed in the Package-info.java file.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.