jdk中提供了javax.xml.bind.JAXBContext、javax.xml.bind.Marshaller、javax.xml.bind.Unmarshaller这几个类,我们可以借用它完成java对象转换为xml格式数据,或者把xml格式文件转换为java对象
具体实例如下
1、新建一个java工程JAXB
2、新建一个包cn.shxjinchao.vo包中创建一个Student.java文件,给类上加注解@XmlRootElement,并且给一个带属性的构造方法,和getter,setter方法
Student.java类,代码如下
--------------------------------------------------------------------------------------------------------------------
package cn.shxjinchao.vo;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Student {
private String name;
private String width;
private String height;
private int age;
public Student() {
super();
}
public Student(String name, String width, String height, int age) {
super();
this.name = name;
this.width = width;
this.height = height;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWidth() {
return width;
}
public void setWidth(String width) {
this.width = width;
}
public String getHeight() {
return height;
}
public void setHeight(String height) {
this.height = height;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", width=" + width + ", height="
+ height + ", age=" + age + "]";
}
}
----------------------------------------------------------------------------------------------------------------------------------
3、创建测试类JaxbTest.java文件
----------------------------------------------------------------------------------------------------------------------------------
package cn.shxjinchao.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import org.junit.Test;
import cn.shxjinchao.vo.Student;
public class JaxbTest {
//获取当前工程目录
private String rootPath=getClass().getResource("/").getFile().toString();
//生成xml文件保存的路径
private String xmlPath = rootPath.substring(1,rootPath.length()-4)+"src/";
private Student st = new Student("高斯林", "65", "170", 57);
@Test
public void test1(){//测试java对象转换为xml
try {
JAXBContext jc = JAXBContext.newInstance(Student.class);
Marshaller ms = jc.createMarshaller();
ms.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");// //编码格式
ms.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);//是否格式化
System.out.println(st.getClass().getSimpleName().toLowerCase());
OutputStream os = new FileOutputStream(new File(xmlPath+st.getClass().getSimpleName().toLowerCase()+".xml"));
ms.marshal(st, os);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (JAXBException e) {
e.printStackTrace();
}
}
@Test
public void test2(){//测试xml转换为java对象
try {
InputStream is = new FileInputStream(new File(xmlPath+st.getClass().getSimpleName().toLowerCase()+".xml"));
JAXBContext jc = JAXBContext.newInstance(Student.class);
Unmarshaller unma = jc.createUnmarshaller();
Student st = (Student) unma.unmarshal(is);
System.out.println(st.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
-----------------------------------------------------------------------------------------------------------------------
测试test1()方法,刷新工程会多一个student.xml文件
测试后Student对象转换为student.xml文件
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<student>
<age>57</age>
<height>170</height>
<name>高斯林</name>
<width>65</width>
</student>
测试test2()方法结果如下
控制台打印结果
Student [name=高斯林, width=65, height=170, age=57]