First, define two example classes ClassA and ClassB for subsequent example demonstrations
The code copy is as follows:
package cn.lzrabbit;
public class ClassA {
private int classAId;
private String classAName;
private ClassB classB;
public int getClassAId() {
return classAId;
}
public void setClassAId(int classAId) {
this.classAId = classAId;
}
public String getClassAName() {
return classAName;
}
public void setClassAName(String classAName) {
this.classAName = classAName;
}
public ClassB getClassB() {
return classB;
}
public void setClassB(ClassB classB) {
this.classB = classB;
}
}
ClassA
The code copy is as follows:
package cn.lzrabbit;
public class ClassB {
private int classBId;
private String classBName;
public int getClassBId() {
return classBId;
}
public void setClassBId(int classBId) {
this.classBId = classBId;
}
public String getClassBName() {
return classBName;
}
public void setClassBName(String classBName) {
this.classBName = classBName;
}
}
ClassB
XmlUtil for serialization
The code copy is as follows:
package cn.lzrabbit;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.bind.*;
public class XmlUtil {
public static String toXML(Object obj) {
try {
JAXBContext context = JAXBContext.newInstance(obj.getClass());
Marshaller 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
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, false);// Whether to omit the xm header declaration information
StringWriter writer = new StringWriter();
marshaller.marshal(obj, writer);
return writer.toString();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@SuppressWarnings("unchecked")
public static <T> T fromXML(String xml, Class<T> valueType) {
try {
JAXBContext context = JAXBContext.newInstance(valueType);
Unmarshaller unmarshaller = context.createUnmarshaller();
return (T) unmarshaller.unmarshal(new StringReader(xml));
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
}
XmlUtil
The call is as follows:
The code copy is as follows:
package cn.lzrabbit;
public class MainRun {
/**
* @param args
*/
public static void main(String[] args) {
ClassB classB = new ClassB();
classB.setClassBId(22);
classB.setClassBName("B2");
ClassA classA = new ClassA();
classA.setClassAId(11);
classA.setClassAName("A1");
classA.setClassB(classB);
System.out.println(XmlUtil.toXML(classA));
}
}
MainRun
The output result is as follows:
The code copy is as follows:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<classA>
<classAId>11</classAId>
<classAName>A1</classAName>
<classB>
<classBId>22</classBId>
<classBName>B2</classBName>
</classB>
</classA>
Here are some points to note
1. Add the @XmlRootElement annotation to the serialized class, otherwise an error will be reported (the error prompt is very clear, so I won't post it here)
2JAXB serializes getters and setters by default when serializing XML, and getters and setters must appear in pairs before they are serialized.
3 attribute names. The default serialized class and attribute names are converted to lowercase by default. If you need to control the attribute names, you need to use @XmlElement(name="ClassAId") to specify the name on the getter or setter. What you need to pay attention to here is @XmlElement can be placed on getter or setter, but only one can be placed, which means that @XmlElement annotation cannot be used on getter and setter at the same time
4How to control the root node name?
Use @XmlRootElement to specify the name attribute, such as @XmlRootElement(name="ClassA")
5 How to add a namespace to specify the namespace attribute using @XmlRootElement(namespace="cn.lzrabbit")
6 How to accurately control each attribute name
Automatically converting JAXB to lowercase first letter will cause unpredictable attribute names to appear. If you don’t bother, set @XmlElement(name="") for each attribute. If you want to save trouble, use Field
7 How to implement serialization using Field fields instead of setters and getters
Add the @XmlAccessorType(XmlAccessType.FIELD) annotation on the class you want to use and specify it as XmlAccessType.FIELD. It is highly recommended to use the @XmlAccessorType(XmlAccessType.FIELD) annotation because you can accurately control the name of each element. Instead of setting the @XmlElement(name="") annotation for each attribute, of course, you can also use the @XmlElement annotation on the Field
The following is a code example using the above annotation
The code copy is as follows:
@XmlRootElement(namespace="cn.lzrabbit")
@XmlAccessorType(XmlAccessType.FIELD)
public class ClassA {
private int classAId;
@XmlElement(name="ClassAName")
private String classAName;
private ClassB classB;
public int getClassAId() {
return classAId;
}
public void setClassAId(int classAId) {
this.classAId = classAId;
}
public String getClassAName() {
return classAName;
}
public void setClassAName(String classAName) {
this.classAName = classAName;
}
public ClassB getClassB() {
return classB;
}
public void setClassB(ClassB classB) {
this.classB = classB;
}
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class ClassB {
private int ClassBId;
private String ClassBName;
public int getClassBId() {
return ClassBId;
}
public void setClassBId(int classBId) {
this.ClassBId = classBId;
}
public String getClassBName() {
return ClassBName;
}
public void setClassBName(String classBName) {
this.ClassBName = classBName;
}
}
The output xml is
The code copy is as follows:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:classA xmlns:ns2="cn.lzrabbit">
<classAId>11</classAId>
<ClassAName>A1</ClassAName>
<classB>
<ClassBId>22</ClassBId>
<ClassBName>B2</ClassBName>
</classB>
</ns2:classA>
PS: Here are a few online tools for your reference:
Online XML/JSON mutual conversion tool:
http://tools.VeVB.COM/code/xmljson
Format XML online/compress XML online:
http://tools.VeVB.COM/code/xmlformat
XML online compression/formatting tools:
http://tools.VeVB.COM/code/xml_format_compress