码迷,mamicode.com
首页 > 编程语言 > 详细

把Java对象转为xml格式

时间:2015-04-22 23:33:26      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:

主要使用到的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());

把Java对象转为xml格式

标签:

原文地址:http://www.cnblogs.com/haoke/p/4448919.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!