Jackson用于Bean和XML之间转换

前端之家收集整理的这篇文章主要介绍了Jackson用于Bean和XML之间转换前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

  Jackson是一个强大工具,可用于Json、XML、实体之间的相互转换。此篇博客主要着重于 实体和XML之间的转换。

XML转换核心

  
   要想灵活地进行转换,需要在实体上使用到Jackson提供的四个注解
   @JacksonXmlElementWrapper:可用于指定List等集合类,外围标签名;
   @JacksonXmlProperty:指定包装标签名,或者指定标签内部属性名;
   @JacksonXmlRootElement:指定生成xml根标签的名字;
   @JacksonXmlText:指定当前这个值,没有xml标签包裹。

实例展示

XML结构:一个班级—>一个老师(卡卡西)—>三个学生(小樱,佐助,鸣人)
班级类:

@JacksonXmlRootElement(localName = "Class")
public class Group {
    Teacher teacher;  //教师
    @JacksonXmlElementWrapper(localName = "Students")
    @JacksonXmlProperty(localName = "Stu")
    List<Student> student;  //学生列表

    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;
    }
}

教师类:卡卡西

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;
    }

}

教师类型: 严厉老师 or 慈祥老师

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;
}

}

学生类: 小樱、佐助、鸣人

public class Student {

    @JacksonXmlProperty(isAttribute = true,localName = "stu_id")
    private String id;  //学号---属性
    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;
    }
}

main方法进行测试

public class JacksonXMLTest {
    public static void main(String[] args) throws JsonProcessingException {

        XmlMapper xmlMapper = new XmlMapper();  
        xmlMapper.setDefaultUseWrapper(false);
        //字段为null,自动忽略,不再序列化
        xmlMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        //XML标签名:使用骆驼命名的属性名,  
        xmlMapper.setPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE); 
        //设置转换模式 
        xmlMapper.enable(MapperFeature.USE_STD_BEAN_NAMING);  

        //序列化 bean--->xml
        Group group = new Group();  //忍者班级

        Teacher teacher = new Teacher();
        teacher.setTeacherTypeCode(new TeacherType("0","严师"));
        teacher.setName("卡卡西");
        teacher.setAge("25");
        teacher.setGender("1");

        Student student1 = new Student();
        student1.setId("001");  //学号
        student1.setName("鸣人");
        student1.setAge("18");
        student1.setGender("1");

        Student student2 = new Student();
        student2.setId("002");  //学号
        student2.setName("佐助");
        student2.setAge("18");
        student2.setGender("1");

        Student student3 = new Student();
        student3.setId("003");  //学号
        student3.setName("小樱");
        student3.setAge("18");
        student3.setGender("0");

        group.setTeacher(teacher);
        group.setStudent(Arrays.asList(student1,student2,student3));

        String result = xmlMapper.writeValueAsString(group);
        System.out.println("序列化结果:" + result);

       }
}

序列化后的XML

参考资料

Jackson官网xml文档
Jackson_XML注解

原文链接:https://www.f2er.com/xml/293605.html

猜你在找的XML相关文章