欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JAXB使用

程序员文章站 2022-03-20 18:04:47
...

使用jaxb,可以简单的在java对象和xml之间的转换,具体参考javaAPI文档里面的软件包 javax.xml.bind。

需要了解三个类:JAXBContext,Unmarshaller,Marshaller。

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Student {
    private  String name;
    private String gender;
    private int age;
    public Student(String name, String gender, int age) {
        super();
        this.name = name;
        this.gender = gender;
        this.age = age;
    }
    public Student() {
        super();
        // TODO Auto-generated constructor stub
    }
    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 int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Student [name=" + name + ", gender=" + gender + ", age=" + age
                + "]";
    }

}
import java.util.List;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name= "studnets")
public class Lstudent {
    private List<Student> students;
    private String name ;
    public Lstudent(){

    }
    @XmlAttribute
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Student> getStudents() {
        return students;
    }
    public void setStudents(List<Student> students) {
        this.students = students;
    }

}
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name ="AA")

public class Tw {
    private  String aString;
    private String bString;
    private String cString;
    public Tw() {
    }
    public Tw(String aString, String bString, String cString) {
        super();
        this.aString = aString;
        this.bString = bString;
        this.cString = cString;
    }
    @XmlElement
    public String getaString() {
        return aString;
    }
    public void setaString(String aString) {
        this.aString = aString;
    }
    @XmlAttribute
    public String getbString() {
        return bString;
    }
    public void setbString(String bString) {
        this.bString = bString;
    }
    @XmlAttribute(name="passwd")
    public String getcString() {
        return cString;
    }
    public void setcString(String cString) {
        this.cString = cString;
    }

    }
public class T_student_Test {
    @Test
    public void test1() throws Exception{
        //JAXBContext 类提供到 JAXB API 的客户端入口点。它提供了管理实现 JAXB 绑定框架操作所需的 XML/Java 绑定信息的抽象,这些操作包括:解组、编组和验证。
        JAXBContext jaxbContext = JAXBContext.newInstance(Student.class);
        Student student = new Student("小明", "男", 20);
        //Marshaller 类负责管理将 Java 内容树序列化回 XML 数据的过程
        Marshaller createMarshaller = jaxbContext.createMarshaller();
        createMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        File file = new File("d:\\student123.xml");
        if(!file .exists()){
            file.createNewFile();
        }
        createMarshaller.marshal(student,new FileOutputStream(file));
        //Unmarshaller 类使客户端应用程序能够将 XML 数据转换为 Java 内容对象树。
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        Student student2 = (Student)unmarshaller.unmarshal(file);
        System.out.println(student2.toString());
    }
    @Test
    public void test2() throws Exception{
        Tw tw = new Tw();
        tw.setaString("第一个元素");
        tw.setbString("第二个元素");
        tw.setcString("第三个元素");

        JAXBContext.newInstance(Tw.class).createMarshaller().marshal(tw , System.out);

    }
    @Test
    public void test3() throws Exception{
        List<Student> list = new ArrayList<Student>();
        list.add(new Student("大白", "男", 23));
        list.add(new Student("小白", "男", 13));
        list.add(new Student("小美", "女", 18));
        Lstudent tt = new Lstudent();
        tt.setName("abc");
        tt.setStudents(list);
        Marshaller marshaller = JAXBContext.newInstance(Lstudent.class).createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(tt , System.out);
    }
}

里面有三个注解@XmlRootElement,@XmlElement,@XmlElement
见名都知道是什么意思,所以也没有什么好说的。还是多动手才印象深刻。