JAXB (Java API for XML Binding) provides a quick and easy way to convert Java objects to and from XML. In Jax-WS (One of Java's WebService specifications), JDK1.6 comes with version jax-ws2.1, with the underlying support being JAXB.
JAXB can implement the conversion of Java objects to XML, 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 annotating it in a Java class, that is, annotating the Java class with annotations that define how the class is converted to XML, how it is transformed, and how an XML is parsed into the object defined by the class. You can also use the JAXB XJC tool to implement Java object binding with XML by defining the schema.
Here's a look at how to complete the Marshal and unmarshal process by annotating annotations.
First look at a small example:
Define a Java class
1 PackageJAXB;2 3 Importjavax.xml.bind.annotation.XmlElement;4 Importjavax.xml.bind.annotation.XmlRootElement;5 ImportJavax.xml.bind.annotation.XmlType;6 7@XmlType (name = "", Proporder = {8"Name",9"Age",Ten"id" One }) A@XmlRootElement (name= "Student") - Public classStudent - { the@XmlElement (name= "ID") - Public intID = 1; -@XmlElement (name= "name") - PublicString name = "LDD"; +@XmlElement (name= "Age") - Public intAge = 18; + @Override A PublicString toString () at { - return"Student [id=" + ID + ", name=" + name + ", age=" + Age + "]"; - } -}
Java to XML (Marshal)
1 PackageJAXB;2 3 ImportJavax.xml.bind.JAXBContext;4 Importjavax.xml.bind.JAXBException;5 ImportJavax.xml.bind.Marshaller;6 7 Public classJavatoxml8 {9 Public Static voidMain (string[] args)throwsjaxbexceptionTen { OneJaxbcontext context = Jaxbcontext.newinstance (Student.class); AMarshaller Marshaller =Context.createmarshaller (); -Marshaller.setproperty (marshaller.jaxb_encoding, "utf-8");//encoding Format -Marshaller.setproperty (Marshaller.jaxb_formatted_output,true);//whether to format the generated XML string theMarshaller.setproperty (Marshaller.jaxb_fragment,false);//whether to omit the XML header information (<?xml version= "1.0" encoding= "gb2312" standalone= "yes"?>) -Student Student =NewStudent (); - Marshaller.marshal (student, system.out); - } +}
The output results are 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 PackageJAXB;2 3 ImportJava.io.File;4 5 ImportJavax.xml.bind.JAXBContext;6 Importjavax.xml.bind.JAXBException;7 ImportJavax.xml.bind.Unmarshaller;8 9 Ten Public classXmltojava One { A Public Static voidMain (string[] args)throwsjaxbexception - { -Jaxbcontext context = Jaxbcontext.newinstance (Student.class); theUnmarshaller Unmarshaller =Context.createunmarshaller (); -File File =NewFile ("Src/main/java/student.xml"); -Student Student =(Student) unmarshaller.unmarshal (file); - System.out.println (student); + } -}
In fact, marshal and unmarshal process is not complicated, only need to get Marshaller or Unmarshaller object from Jaxbcontext, you can let JAXB help us to convert. The main things we need to do is to define a rule that tells Jaxb how to convert a class, to what format, to XML, and here are some of the main annotations in JAXB.
|
|
|
@XmlRootElement |
Map a Java class to the root node of an XML |
Name to define this root node namespace defines this root node namespace |
@XmlAccessorType |
Define what types of mappings in this class need to be mapped to XML. |
Xmlaccesstype.field: Mapping All fields in this class to XML Xmlaccesstype.property: Mapping properties in this class (Get/set method) to XML Xmlaccesstype.public_member: Maps the field or property of all public in this class to XML at the same time (default) Xmlaccesstype.none: Not mapped |
@XmlElement |
Specifies a field or Get/set method that maps to the node of the XML. For example, when a class's xmlaccessortype is labeled as a property, the annotation is annotated on a field that does not have a Get/set method, and the field can be mapped to XML. |
DefaultValue specifying a node default value Name specifies the node name Namespace specifying a node namespace Whether the required must be (false by default) nillable Whether the field contains a nillable= "true" property (False by default) Type defines the association types for this field or property |
@XmlAttribute |
Specifies a field or Get/set method that maps to XML properties. |
Name Specifies the property name Namespace specifying a property namespace Whether the required must be (false by default) |
@XmlTransient |
Defining a field or property does not need to be mapped to XML. For example, when a class's xmlaccessortype is labeled as a property, the annotation is annotated on the field of a Get/set method, and the attribute is not mapped. |
|
@XmlType |
Some related rules for defining mappings |
Proporder specifying the order of nodes when mapping XML FACTORYCLASS Specifies the factory class required to generate the Map class instance when Unmarshal is specified, which defaults to the class itself FactoryMethod factory methods for specifying factory classes Name defines the names of the type in the XML Schema Namespace specifying namespaces in the schema |
@XmlElementWrapper |
Defines a parent node for an array element or collection element. For example, an element in a class is a list items, and if you do not add this annotation, 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; Such an XML style will be generated: <items> <item>...</item> <item>...</item> </items> |
|
@XmlJavaTypeAdapter |
Customizes an adapter that maps a field or attribute to XML. For example, if a class contains an interface, we can define an adapter (inherited from the Javax.xml.bind.annotation.adapters.XmlAdapter Class) to specify how the interface maps to XML. |
|
@XmlSchema |
To configure the namespace of the entire package, this annotation needs to be placed in the Package-info.java file. |
|
Java Development WEBSERVICE--JAXB