1. Annotations
Creating nodes and attributes in the Java model that correspond to XML files needs to be represented by annotations
@XmlRootElement
Role: Map a Java class to the root node parameter of an XML:
Name to define this root node
namespace defines this root node namespace
@XmlAccessorType
Role: Defines what types of mappings in this class need to be mapped to XML.
Can receive four parameters, respectively:
Xmlaccesstype.field: Indicates that a non-static (static), non-serialized (transient) attribute in this class is mapped to XML, and there is no Get/set method to
Xmlaccesstype.property: Use the Set/get method to serialize a property or element (get method without annotation auto-mapping)--Description: Whether to add annotations on the Xml,get method by the Get method, but cannot add annotations on attributes
Xmlaccesstype.public_member: The field (field) or property (Get/set pair) of all public in this class is mapped to XML (the default annotation) at the same time. This note contains two annotations for Xmlaccesstype.field and xmlaccesstype.property, so be careful not to add two annotations to the same field
Xmlaccesstype.none: Not mapped
Additional notes:
The default access level for the @XmlAccessorType is xmlaccesstype.public_member, so if the private member variable in the Java object sets the Getter/setter method for the public permission, Do not use @xmlelement and @xmlattribute annotations on private variables, otherwise the same attribute is reported as two errors in the Java class when generating XML from a Java object.
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.
@XmlElements
Represents or, the contents of the list collection can be of the following two types
@XmlElements ({
@XmlElement (name = "Person", type = Person.class),
@XmlElement (name = "String", type = String.class)
})
Private list List;
Supplemental NOTE: If the list uses generics, the list collection can contain only one data type, which is useless when using the annotation.
@XmlElement
Function: 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.
Parameters:
DefaultValue specifying a node default value
Name specifies the node name
Namespace specifying a node namespace
Type defines the association types for this field or property
Whether the required must be (false by default)
nillable Whether the field contains a nillable= "true" property (False by default)
@XmlAttribute
Function: Specifies a field or Get/set method that maps to the attributes of the XML (distinguishes the @xmlelement--node).
Parameters:
Name Specifies the property name
Namespace specifying a property namespace
Whether the required must be (false by default)
@XmlTransient
Role: 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
Role: 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
Function: 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
Role: Customize the 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
Function: Configure the namespace of the entire package, this annotation should be placed in the Package-info.java file
2. Code Examples
2.1 object to XML
public static void Marshallerbyjaxb (String path, Object object) {
try {
Jaxbcontext JC = jaxbcontext.newinstance (Object.getclass ());
Marshaller Marshaller = Jc.createmarshaller ();
Marshaller.setproperty (marshaller.jaxb_encoding, "UTF-8");//Encoding format
Marshaller.setproperty (Marshaller.jaxb_formatted_output, true);//whether to format the generated XML string
Marshaller.setproperty (Marshaller.jaxb_fragment, false);//whether to omit XML header declaration information
File File = new file (path);
if (!file.exists ()) {
File.createnewfile ();
}
Marshaller.marshal (object, file);
} catch (Jaxbexception e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}
}
2.2 XML to Object
public static void Unmarshallerbyjaxb (String path, Object object) {
try {
Jaxbcontext JC = jaxbcontext.newinstance (Object.getclass ());
Unmarshaller Unmarshaller = Jc.createunmarshaller ();
File File = new file (path);
if (!file.exists ()) {
throw new FileNotFoundException ("Can not load XML file!");
}
Unmarshaller.unmarshal (file);
} catch (Jaxbexception e) {
E.printstacktrace ();
} catch (FileNotFoundException e) {
E.printstacktrace ();
}
}
Description: The incoming obj must be of a specific type when the parameter is passed.
such as: first instantiation of person per = new person ();
Re-transmit UNMARSHALLERBYJAXB (path, person);
Can not direct UNMARSHALLERBYJAXB (path, person.class);
3. List, Map collection type description
The collection must be encapsulated inside an object to use JAXB to XML, and the collection cannot be directly converted to XML
3.1 List
List collection or array as long as it is encapsulated inside the object, use @xmlelementwrapper annotations
Note: When there is only one property of the list collection inside the object, you can add a layer of nodes without @xmlelementwrapper annotations, and the object within the list is directly the level two node under the root node.
3.2 Map
such as: Map<string, person> collection, person is a custom object
1. Create a new object (for example: Allperson) As a transit object that contains two attributes, string and person type (the former is the map key, and the latter is the value of map);
2. Use @xmljavatypeadapter annotations to map<string, person> to allperson[], the size of the array is the same size as the map;
Summary: The two methods in the XMLAdapter are useful, take care not to miss out
When Object->xml
Map<string, person>-to allperson[] (Allperson object contains String, person)
When Xml->object
Allperson[] (Allperson object contains String, person)-to map<string, person>
Using JAXB to read and write XML