使用JAXB完成XML与java对象的转换

[b]
SAX 处理超大xml时使用
dom4j 通过xpath来解析非常方便,适合处理较小的xml
Xstream--->Stax 基于流的方式处理
Xstream,Jackson 可以将json与java进行转换
JAXB java表中API,专门负责java与xml的互转
java--->xml:marshaller
xml--->java:unmarshaller
[/b]

[b]JAVB完成对象与XML的互转[/b]

package com.hqh.jaxb;

public class Classroom {
private int id;
private String name;
private int grad;
public Classroom() {
super();
}
public Classroom(int id, String name, int grad) {
super();
this.id = id;
this.name = name;
this.grad = grad;
}
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 getGrad() {
return grad;
}
public void setGrad(int grad) {
this.grad = grad;
}


}




package com.hqh.jaxb;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Student {
private int id;
private String name;
private int age;
private String sex;
private Classroom classroom;

public Student() {
super();
}

public Student(int id, String name, int age, String sex, Classroom classroom) {
super();
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
this.classroom = classroom;
}

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 String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}

public Classroom getClassroom() {
return classroom;
}

public void setClassroom(Classroom classroom) {
this.classroom = classroom;
}


}




package com.hqh.jaxb;


import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

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

import org.junit.Test;

public class JAXBTest {

/**Object--->XML*/
@Test
public void testMarshaller() throws Exception {
//对象
//要使用JAXB转换对象为xml,需要在对应的类上使用@XmlRootElement进行标注
Classroom classroom = new Classroom(1, "一班", 2013);
Student jaxbElement = new Student(1, "张三", 22, "男", classroom);

//将student对象转为xml
JAXBContext ctx = JAXBContext.newInstance(Student.class);
Marshaller marshaller = ctx.createMarshaller();

//将转换后的xml作为标准打印流输出
marshaller.marshal(jaxbElement, System.out);

//将转换后的xml以文件形式输出
File file = new File("src/student.xml");
if(!file.exists()) {
file.createNewFile();
}
marshaller.marshal(jaxbElement, new FileOutputStream(file, false));
}

/**XML--->Object*/
@Test
public void testUnMarshaller() throws Exception {
//XML
InputStream is = this.getClass().getClassLoader().getResourceAsStream("student.xml");

//将student对象转为xml转为student对象
JAXBContext ctx = JAXBContext.newInstance(Student.class);
Unmarshaller unmarshaller = ctx.createUnmarshaller();
Object obj = unmarshaller.unmarshal(is);
Student stu = (Student)obj;
System.out.println(stu.getName()+","+stu.getClassroom().getName());
}
}



将Student对象转换为xml的结果,很方便
[color=blue]如果存在中文转换,将项目的编码设置为UTF-8,不然中文为乱码[/color]

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<student>
<age>22</age>
<classroom>
<grad>2013</grad>
<id>1</id>
<name>一班</name>
</classroom>
<id>1</id>
<name>张三</name>
<sex>男</sex>
</student>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值