JAVA Development Webservice -- JAXB, webservicejaxb

Source: Internet
Author: User

JAVA Development Webservice -- JAXB, webservicejaxb

JAXB (Java API for XML Binding) provides a fast and convenient way to convert Java objects and XML. In JAX-WS (one of Java's WebService specifications), JDK1.6 comes with a version of JAX-WS2.1 whose underlying support is JAXB.

JAXB can implement mutual conversion between Java objects and XML. In JAXB, the process of converting a Java object to XML is called Marshal, and the process of converting XML to Java objects is called UnMarshal. We can bind a Java object to a piece of XML by annotation in the Java class, that is, annotation in the Java class, these annotations define how to convert this class to XML, how to convert it, and how XML is parsed into the objects defined by this class. You can also use the XJC tool of JAXB, you can bind a Java object to an XML object by defining a schema.

The following describes how to complete Marshal and UnMarshal through annotation.

Let's take a look at a small example:

Define a java class

 1 package jaxb; 2  3 import javax.xml.bind.annotation.XmlElement; 4 import javax.xml.bind.annotation.XmlRootElement; 5 import javax.xml.bind.annotation.XmlType; 6  7 @XmlType(name = "", propOrder = { 8   "name", 9    "age",10    "id"11   })12 @XmlRootElement(name="Student")13 public class Student14 {15     @XmlElement(name="ID")16     public int id = 1;17     @XmlElement(name="Name")18     public String name = "ldd";19     @XmlElement(name="Age")20     public int age = 18;21     @Override22     public String toString()23     {24         return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";25     }26 }

Java To XML (Marshal)

1 package jaxb; 2 3 import javax. xml. bind. JAXBContext; 4 import javax. xml. bind. JAXBException; 5 import javax. xml. bind. extends aller; 6 7 public class JavaToXML 8 {9 public static void main (String [] args) throws JAXBException10 {11 JAXBContext context = JAXBContext. newInstance (Student. class); 12 bytes aller extends aller = context. createMarshaller (); 13 bytes aller. setProperty (exploraller. JAXB_ENCODING, "utf- 8 "); // The encoding format is 14 bytes aller. setProperty (exploraller. JAXB_FORMATTED_OUTPUT, true); // whether to format the generated xml string 15 bytes aller. setProperty (exploraller. JAXB_FRAGMENT, false); // whether to omit the xml header information (<? Xml version = "1.0" encoding = "gb2312" standalone = "yes"?>) 16 Student student = new Student (); 17 student aller. marshal (Student, System. out); 18} 19}

The output result is as follows:

<?xml version="1.0" encoding="utf-8" standalone="yes"?><Student>    <Name>ldd</Name>    <Age>18</Age>    <ID>1</ID></Student>

XML To Java (UnMarshal)

 1 package jaxb; 2  3 import java.io.File; 4  5 import javax.xml.bind.JAXBContext; 6 import javax.xml.bind.JAXBException; 7 import javax.xml.bind.Unmarshaller; 8  9 10 public class XmlToJava11 {12     public static void main(String[] args) throws JAXBException13     {14         JAXBContext context = JAXBContext.newInstance(Student.class);15         Unmarshaller unmarshaller = context.createUnmarshaller();16         File file = new File("src/main/java/student.xml");17         Student student = (Student) unmarshaller.unmarshal(file);18         System.out.println(student);19     }20 }

 

In fact, the process of Marshal and UnMarshal is not complicated. You only need to obtain the Marshaller or Unmarshaller object from JAXBContext, so that JAXB can help us with the conversion. The main content we need to operate on is to define a rule to tell JAXB how to convert a class to XML according to the format. Below are some of the main annotations in JAXB.
     
@ XmlRootElement Map a Java class to the root node of an XML segment.

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.

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 the node where a field or get/set method is mapped 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.

 

DefaultValue specifies the default node Value

Name specifies the node name

Namespace specifies the node namespace

Required or not (the default value is false)

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.  

Name specifies the attribute name

Namespace

Required or not (the default value is 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 Rules for defining Mappings  

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 XML Schema

Namespace specifies the namespace in the Schema

@ XmlElementWrapper  

Defines a parent node for an array 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 is included 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.