我好怕自己成为有故事的人,这一天我怕了好久好久,我唯一能做的只有尽人之力。

把Java对象转为xml格式

主要使用到的Java类有:javax.xml.bind.JAXBContext,javax.xml.bind.Marshaller(编排)

代码主要展示如下:

public class Student {

    private int id;
    private String name;
    private int age;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public Student(int id, String name, int age) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
    }
    public Student() {
        super();
        // TODO Auto-generated constructor stub
    }
}

 

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class ClassRoom {
    private int classRoomId;
    private String classRoomName;
    private Student student;
    public int getClassRoomId() {
        return classRoomId;
    }
    public void setClassRoomId(int classRoomId) {
        this.classRoomId = classRoomId;
    }
    public String getClassRoomName() {
        return classRoomName;
    }
    public void setClassRoomName(String classRoomName) {
        this.classRoomName = classRoomName;
    }
    public Student getStudent() {
        return student;
    }
    public void setStudent(Student student) {
        this.student = student;
    }
    public ClassRoom(int classRoomId, String classRoomName, Student student) {
        super();
        this.classRoomId = classRoomId;
        this.classRoomName = classRoomName;
        this.student = student;
    }
    public ClassRoom() {
        super();
        // TODO Auto-generated constructor stub
    }

}

测试:

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args)throws Exception{
        // TODO Auto-generated method stub

        JAXBContext jbt = JAXBContext.newInstance(ClassRoom.class);
        
        ClassRoom cr = new ClassRoom(1, "一班", new Student(10, "张三", 22));
        
        Marshaller ms = jbt.createMarshaller();
        
        ms.marshal(cr, System.out);
        
        
    }

}

结果:<?xml version="1.0" encoding="UTF-8" standalone="yes"?><classRoom><classRoomId>1</classRoomId><classRoomName>一班</classRoomName><student><age>22</age><id>10</id><name>张三</name></student></classRoom>

在这个简单的示例中主要留意的地方:1.Test.java 中的代码

2. ClassRoom 类中加注解@XmlRootElement

 

把生成的xml文件生成Java对象:

 

主要代码:

String xml ="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><classRoom><classRoomId>1</classRoomId><classRoomName>一班</classRoomName><student><age>22</age><id>10</id><name>张三</name></student></classRoom>";
        
        JAXBContext unjbt = JAXBContext.newInstance(ClassRoom.class);
        
        Unmarshaller unms = unjbt.createUnmarshaller();
        
        ClassRoom uncr =(ClassRoom) unms.unmarshal(new StringReader(xml));
        
        System.out.println(uncr.getClassRoomId()+uncr.getClassRoomName());
posted @ 2015-04-22 22:30  goLazy  阅读(8616)  评论(0编辑  收藏  举报
你是否也有自己的故事?埋藏在自己心的最深的地方。