Using JAXB to complete the conversion of object and XML

Source: Internet
Author: User
Tags map class

1. Create the corresponding Java model for each node and attribute information in the XML file.

2. The creation of nodes and attributes corresponding to XML files in the Java model needs to be represented by annotations.

@XmlRootElement Map a Java class to the root node of an XML
Parameters:
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. Can receive four parameters, respectively:

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 A node that specifies a field or Get/set method to map to 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.
Parameters:
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 Specify a field or Get/set method to map to the attributes of the XML.
Parameters:
Name Specifies the property name
namespace specifying a property namespace
whether the required must be (false by default)

@XmlTransient Define a field or property that 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
Parameters:
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 Define 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 Customize an adapter that maps a field or property 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 Configure the namespace of the entire package, this annotation needs to be placed in the Package-info.java file
<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>

3. Nonsense not much to say, we directly on a very simple example of the program.

3.1 Two basic Pojo classes.

Package Com.npf.jaxb;public class Classroom {private int id;private String name;}

Package Com.npf.jaxb;import javax.xml.bind.annotation.XmlRootElement; @XmlRootElementpublic class Student {private int id;private String name;private int age;private Classroom;
}

3.2 Test methods

Package Com.npf.jaxb;import Java.io.stringreader;import Javax.xml.bind.jaxbcontext;import Javax.xml.bind.jaxbexception;import Javax.xml.bind.marshaller;import Javax.xml.bind.unmarshaller;import Org.junit.test;public class Jaxbtest {@Testpublic void Object2xml () throws Jaxbexception{jaxbcontext Jaxbcontext = Jaxbcontext.newinstance (Student.class);        Marshaller Marshaller = Jaxbcontext.createmarshaller (); Marshaller.setproperty (marshaller.jaxb_encoding, "UTF-8"); Marshaller.setproperty (Marshaller.jaxb_formatted_output, true); Student Student = new Student (1, "Zhangsan", 1,new classroom (1, "Computer")); Marshaller.marshal (Student, System.out);} @Testpublic void Xml2object () throws Jaxbexception{jaxbcontext Jaxbcontext = Jaxbcontext.newinstance (Student.class); Unmarshaller Unmarshaller = Jaxbcontext.createunmarshaller (); String xml = "<?xml version=\" 1.0\ "encoding=\" utf-8\ "standalone=\" yes\ "?>" + "<student><age>1</ Age><id>1</id><name>zhangsan</name><room><id>1</id> "+" <name>computer</name></room></student> "; Student Student = (Student) Unmarshaller.unmarshal (new StringReader (XML)); System.out.println (Student.getname () + "," +student.getroom (). GetName ());}}

Using JAXB to complete the conversion of object and XML

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.