A detailed explanation of the use of JAXB annotations

Source: Internet
Author: User

First, what is JAXB?

JAXB, or Java architecturefor XML Binding, is an industry standard that is a technology that can produce Java classes based on XML schemas. In this process, JAXB also provides a way to reverse-generate the Java object tree from the XML instance document, and can re-write the contents of the Java object tree to the XML instance document.

Ii. Introduction of annotations

1. The common annotations in JAXB that deal with conversion between Java and XML are:

1) @XmlType

2) @XmlElement

3) @XmlRootElement

4) @XmlAttribute

5) @XmlAccessorType

6) @XmlAccessorOrder

7) @XmlTransient

8) @XmlJavaTypeAdapter

1. Common annotation Usage Instructions

1) @XmlType

This annotation is used on class classes and is often used in conjunction with @xmlrootelement, @XmlAccessorType. It has three properties: Name, Proporder, namespace, and only the first two properties that are often used.   Example: [Java] view plain copy @XmlType (name = "Basicstruct", Proporder = {"Intvalue", "Stringarray", "StringValue") public class mytype{...}

When you use the Proporder property of @xmltype, you must list all the attributes in the JavaBean object (and also add an XML annotation to all properties), or you will get an error.

2) @XmlElement

This annotation is used on the properties of the Java class to map the attribute to the child node of the XML. You can change the name of the Java attribute in the XML file by configuring the Name property value later. such as: [Java] view plain copy @XmlElement (name= "Address") private String youraddress;

When configured here, the nodes in the XML file are < address></Address>, rather than < youraddress/>.

3) @XmlRootElement

Class-level annotations, corresponding to the root node in the XML file. Often used in conjunction with @XmlType and @XmlAccessorType. Example: [Java] view plain copy @XmlType @XmlAccessorType (Xmlaccesstype.field) @XmlRootElement public class Addre SS {}

4) @XmlAttribute

Used to map the properties of a Java object to an XML property, and to specify an alias for the generated XML property through the Name property. such as: [Java] view plain copy @XmlAttribute (name= "country") private String state;

5) @XmlAccessorType

@XmlAccessorType is used to specify how Java object properties are accessed when an XML file is generated by a Java object. Often used in conjunction with @xmlrootelement, @XmlType. Its property value is the 4 enumerated values of Xmlaccesstype, respectively:

  Xmlaccesstype.field:All member variables in the Java object;

  Xmlaccesstype.property:All member variables in a Java object that are accessed through Getter/setter mode;

  Xmlaccesstype.public_member:A member variable of all public access rights in a Java object and a member variable accessed by means of getter/setter;

  xmlaccesstype.none:All properties of a Java object are not mapped to elements of XML.

Note: The default access level for @XmlAccessorType is xmlaccesstype.public_member. Therefore, if the private member variable in the Java object sets the Getter/setter method of the public permission, do not use the @xmlelement and @xmlattribute annotations on the private variable. Otherwise, when generating XML from a Java object, it is reported that the same property has two errors in the Java class. Similarly, if @xmlaccessortype access is xmlaccesstype.none, these member variables can still be mapped to XML files if @xmlelement or @xmlattribute annotations are used on member variables in Java.

6) @XmlAccessorOrder

Used to sort the XML elements that are generated by the Java object. It has two attribute values:

  accessororder.alphabetical: Sorts the generated XML elements alphabetically;

 xmlaccessorder.undefined: not sorted.

7) @XmlTransient

Used to indicate that this property is ignored when mapping XML by a Java object. That is, this element does not appear in the generated XML file.

8) @XmlJavaTypeAdapter

@XmlJavaTypeAdapter are commonly used when converting more complex objects, such as map types or formatted dates. When using this annotation, you need to write a adapter class to inherit the XMLAdapter abstract class and implement the method inside.

@XmlJavaTypeAdapter (Value=xxx.class), value defines the adapter class for itself

The

XMLAdapter is as follows: [Java] view plain copy public abstract class xmladapter< valuetype,boundtype> {       // do-nothing constructor for  the derived classes.       protected xmladapter ()  {}        // convert a value type to a bound  type.       public abstract boundtype unmarshal (ValueType &NBSP;V);       // Convert a bound type to a  value type.       public abstract valuetype marshal (BoundType &NBSP;V);    } 

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.