JAXB and jaxb Parse xml

Source: Internet
Author: User

JAXB and jaxb Parse xml

JAXB (Java Architecture for XML Binding) is an industry standard and a technology that can generate Java classes based on XML Schema. In this process, JAXB also provides a way to reverse generate a Java object tree for the XML instance document, and re-write the content of the Java object tree to the XML instance document.

On the other hand, JAXB provides a fast and easy way to bind the XML schema to the Java representation, so that Java developers can easily combine XML data and processing functions in Java applications.

JDKJAXBRelated important classesAnd Interface:

The JAXBContext class is an application entry used to manage binding information in XML/Java.

The Marshaller interface serializes Java objects into XML data.

Unserializedaller interface to deserialize XML data into Java objects.

In JAXB, the process of converting a Java object to XML is called Marshal, and the process of converting XML to a Java object is called UnMarshal. We can bind a Java object to a piece of XML by annotation in the Java class. That is to say,

Note some annotations in the Java class, which define how to convert the class to XML, how to convert it, and how to parse an XML file into the object defined by this class.

JAXBMain notesSolution

@ XmlRootElement maps a Java class to a root node of an XML segment.

Parameter: name defines the name of the Root Node

Namespace defines the root node namespace

@ XmlAccessorType defines the types in the ing class that need to be mapped to XML. You can receive four parameters:

XmlAccessType. FIELD: maps all fields in this class to XML

XmlAccessType. PROPERTY: maps the attributes in this class (get/set Method) to XML

XmlAccessType. PUBLIC_MEMBER: maps all public fields or properties in this class to XML at the same time (default)

XmlAccessType. NONE: No ing

@ XmlElement specifies a node where a field or get/set method maps to XML. For example, when the XmlAccessorType of a class is labeled as PROPERTY, you can map the field to XML by marking this annotation on a field without the get/set method.

Parameter: defaultValue specifies the default node value.

Name specifies the node name

Namespace specifies the node namespace

RequiredRequired or not (default:False, SolutionNullOrNull ValueLabel not displayed)

  Nillable whether the field contains the nillable = "true" attribute (default value: false)

Type defines the association type of this field or attribute

@ XmlAttribute specifies the attribute mapped to XML by a field or the get/set method.

Parameter: name specifies the attribute name.

Namespace
  RequiredRequired or not (default:False)

@ XmlTransient defining a field or attribute does not need to be mapped to XML. For example, if the XmlAccessorType of a class is marked as PROPERTY and this annotation is marked on the field of a get/set method, the PROPERTY will not be mapped.

@ XmlType: some rules for defining Mappings

Parameter: propOrder specifies the node sequence for XML ing.

FactoryClass specifies the factory class required to generate the ing class instance when UnMarshal is specified. The default value is the class itself.

FactoryMethod specifies the factory method of the factory class

Name defines the name of type in XMLSchema

Namespace specifies the namespace in the Schema

@ XmlElementWrapper defines a parent node for an array element or set element. For example, an element in the class is List items. If this annotation is not added, the element will be mapped

<Items>... </items>

<Items>... </items>

In this form, this annotation can wrap this element, such:

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

The following XML style will be generated:

<Items>

<Item>... </item>

<Item>... </item>

</Items>

@ XmlJavaTypeAdapter: a custom adapter that maps a field or attribute to XML. For example, the class contains an interface. We can define an adapter (inherited from the javax. xml. bind. annotation. adapters. XmlAdapter class) to specify how this interface maps to XML.

@ XmlSchema configure the namespace of the entire package, which must 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.