把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());

转载于:https://www.cnblogs.com/haoke/p/4448919.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值