Java USES jaxb to manipulate XML samples
- 2020-04-01 03:14:09
- OfStack
First, define two sample classes ClassA and ClassB for subsequent sample demonstrations
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
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
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");// // Coding format
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);//Whether to format the generated XML string
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, false);//Whether to omit 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:
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 results are 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 a few things to note
1 class to serialize with the @xmlrootelement annotation, otherwise an error will be reported (the error message is too clear to be posted here)
2JAXB serializes XML The default serializes getters and setters, and the getters and setters must appear in pairs to be serialized
The default serialized class and property names are first converted to lowercase by default. If you want to control the property name, you need to specify the name using @xmlelement (name="ClassAId") on the getter or setter
How do I control the root node name?
Use the @xmlrootelement to specify the name attribute, such as @xmlrootelement (name="ClassA")
How do I add a namespace
Specify the namespace attribute using the @xmlrootelement (namespace="cn.lzrabbit")
How exactly do you control the name of each property
JAXB automatically converts lowercase to the first letter, which results in unpredictable property names, so you can set @xmlelement (name="") for each property if you want, and use Field if you want to save the trouble
How do I implement serialization using the Field Field instead of the setter and getter
Add the @xmlaccessortype (xmlaccesstype.field) annotation to the class you want to use and specify it as xmlaccesstype.field. The @xmlaccessortype (xmlaccesstype.field) annotation is highly recommended because you can control the name of each element precisely without having to set the @xmlelement (name="") annotation for each attribute. You can also use the @xmlelement annotation on the Field
The following code example USES the above annotations
@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;
}
}
XML output for
<?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 more online tools about XML operation for your reference:
Online XML/JSON interconversion tool:
(link: http://tools.jb51.net/code/xmljson)
Online formatted XML/ online compressed XML:
(link: http://tools.jb51.net/code/xmlformat)
XML online compression/formatting tools:
(link: http://tools.jb51.net/code/xml_format_compress)