Overview
Jackson is a powerful tool that can be used to convert JSON, XML, and entities to each other. This blog mainly focuses on the transformation between entity and XML. XML Transformation Core
To be flexible, you need to use the four annotations that Jackson provided on the entity:
@JacksonXmlElementWrapper: Can be used to specify lists and other collection classes, peripheral label names;
@JacksonXmlProperty: Specify the label name of the wrapper, or specify the label's internal property name;
@JacksonXmlRootElement: Specifies the name of the generated XML root tag;
@JacksonXmlText: Specifies the current value, without an XML tag package. Example Show
XML Structure: One teacher (Kakashi), three students (Sakura, assistant, Naruto)
Class Category:
@JacksonXmlRootElement (LocalName = "class") public
Class Group {
Teacher Teacher; Teacher
@JacksonXmlElementWrapper (localname = "Students")
@JacksonXmlProperty (localname = "Stu")
list< Student> Student; Student list public
Teacher Getteacher () {
return Teacher;
}
public void Setteacher (Teacher Teacher) {
this.teacher = Teacher;
}
Public list<student> getstudent () {
return Student;
}
public void Setstudent (list<student> Student) {
this.student = Student;
}
}
Teacher class: Kaka West
public class Teacher {
@JacksonXmlProperty (localname = "TypeCode")
private teachertype Teachertypecode;
private String name;
private String gender;
Private String age;
Public Teachertype Getteachertypecode () {
return teachertypecode;
}
public void Setteachertypecode (Teachertype teachertypecode) {
this.teachertypecode = Teachertypecode;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
this.name = name;
}
Public String Getgender () {
return gender;
}
public void Setgender (String gender) {
this.gender = gender;
}
Public String Getage () {
return age;
}
public void Setage (String age) {
this.age = age;
}
}
teacher Type: Stern teacher or kindly teacher
public class Teachertype {
@JacksonXmlProperty (Isattribute = true, LocalName = "type")
private String type;
@JacksonXmlText
private String value;
Public Teachertype () {
} public
Teachertype (string type, String value) {
this.type = type;
This.value = value;
}
Public String GetType () {
return type;
}
public void SetType (String type) {
this.type = type;
}
}
Student Category: Sakura, adjuvant, Naruto
public class Student {
@JacksonXmlProperty (Isattribute = true, LocalName = "stu_id")
private String ID; Study number---attribute
private String name;
private String gender;
Private String age;
Public String getId () {
return ID;
}
public void SetId (String id) {
this.id = ID;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
this.name = name;
}
Public String Getgender () {
return gender;
}
public void Setgender (String gender) {
this.gender = gender;
}
Public String Getage () {
return age;
}
public void Setage (String age) {
this.age = age;
}
}
The Main method for testing
public class Jacksonxmltest {public static void main (string[] args) throws Jsonprocessingexception {XMLM
Apper xmlmapper = new Xmlmapper ();
Xmlmapper.setdefaultusewrapper (FALSE);
The field is NULL, automatically ignored, and is no longer serialized xmlmapper.setserializationinclusion (JsonInclude.Include.NON_NULL);
XML tag name: attribute name named with Camel, Xmlmapper.setpropertynamingstrategy (propertynamingstrategy.upper_camel_case);
Set the conversion Mode xmlmapper.enable (mapperfeature.use_std_bean_naming); Serialized Bean--->xml Group group = new Group ();
Ninja class Teacher Teacher = new Teacher ();
Teacher.setteachertypecode (New Teachertype ("0", "strict teacher"));
Teacher.setname ("Kaka West");
Teacher.setage ("25");
Teacher.setgender ("1");
Student student1 = new Student (); Student1.setid ("001");
Study number Student1.setname ("Naruto");
Student1.setage ("18");
Student1.setgender ("1");
Student Student2 = new Student (); Student2.setid ("002");
Study number Student2.setname ("adjuvant");
Student2.setage ("18");
Student2.setgender ("1");
Student Student3 = new Student (); Student3.setid ("003");
Study number Student3.setname ("Sakura");
Student3.setage ("18");
Student3.setgender ("0");
Group.setteacher (teacher);
Group.setstudent (Arrays.aslist (STUDENT1,STUDENT2,STUDENT3));
String result = xmlmapper.writevalueasstring (group);
System.out.println ("Serialization Result:" + result);
}
}
serialized XML
References
Jackson official website XML document
Jackson_xml annotations