标签:each avr 反序列化 模式 var gre utf-8 article org
JAXB是Java Architecture for XML Binding的缩写,用于在Java类与XML之间建立映射,可以帮助开发人员非常方便的將XML和Java对象进行相互转换。
本文以一个简单的样例介绍JAXB的使用。首先我们须要了解一下JAXB经常使用的API。
Unmarshaller接口,将XML数据反序列化为Java对象。
@XmlType,将Java类或枚举类型映射到XML模式类型
其它值还有XmlAccessType.PROPERTY和XmlAccessType.NONE。
我们须要进行绑定的Java Bean内容例如以下:
Employee.java
package net.csdn.beans;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
@XmlType(name = "Employee", propOrder = { "name", "age", "role", "gender" })
public class Employee {
private String name;
private String gender;
private int age;
private String role;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
@Override
public String toString() {
return "Employee:: Name=" + this.name + " Age=" + this.age + " Gender="
+ this.gender + " Role=" + this.role;
}
}
须要转换为Java对象的XML文件内容例如以下:
employee.xml
<?xml version="1.0"?>
<employee id="1">
<name>Pankaj</name>
<age>29</age>
<role>Java Developer</role>
<gender>Male</gender>
</employee>
接下来编写測试用例代码:
TestJAXB.java
package net.csdn.test;
import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import net.csdn.beans.Employee;
import org.junit.Test;
public class TestJAXB {
@Test
public void testXml2Obj() throws Exception {
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("employee.xml");
byte[] bytes = new byte[is.available()];
is.read(bytes);
String xmlStr = new String(bytes);
JAXBContext context = JAXBContext.newInstance(Employee.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
Employee emp = (Employee) unmarshaller.unmarshal(new StringReader(xmlStr));
System.out.println(emp);
}
@Test
public void testObj2Xml() {
Employee emp = new Employee();
emp.setAge(10);
emp.setGender("Male");
emp.setName("Jane");
emp.setRole("Teacher");
String xmlStr = TestJAXB.convertToXml(emp,"utf-8");
System.out.println(xmlStr);
}
public static String convertToXml(Object obj, String encoding) {
String result = null;
try {
JAXBContext context = JAXBContext.newInstance(obj.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
StringWriter writer = new StringWriter();
marshaller.marshal(obj, writer);
result = writer.toString();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
执行testObj2Xml測试方法。控制台输出:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<employee>
<name>Jane</name>
<age>10</age>
<role>Teacher</role>
<gender>Male</gender>
</employee>
执行testXml2Obj測试方法。控制台输出:
Employee:: Name=Pankaj Age=29 Gender=Male Role=Java Developer
注:本例中使用JUnit4作为单元測试工具,在Eclipse中点击Window->Show View->OutLine菜单打开outline视图,分别在testXml2Obj和testObj2Xml方法上点击右键->Run As->JUnit Test就可以。
Java&Xml教程(十一)JAXB实现XML与Java对象转换
标签:each avr 反序列化 模式 var gre utf-8 article org
原文地址:http://www.cnblogs.com/cxchanpin/p/7074322.html