JAXB目前已经集成到了JDK6中,但是还是推荐下载使用最新版的jaxb库, https://jaxb.dev.java.net/
从JAXB2.0开始,可以通过使用annotation进行Object-XMl mapping。
本文将通过以下步骤演示如何使用JAXB annotation绑定使用了namespace的XML文件
1. 创建employees.xml文件
2. 创建Employees.java和Employee.java绑定employees.xml
3. 创建package-info.java和jaxb.index文件
4. 创建XMLParser.java封装marshal/unmarshal操作
5. 创建Test.java运行测试
现在将逐步展开演示
1. 创建employees.xml文件
这是一个很普通的xml文件,用于展示组织内部的成员(employee)状况,这里有一点需要注意的是使用了namespace,这也是本文将要演示的重点。
2. 创建Employees.java和Employee.java绑定employees.xml
package com.javaeye.terrencexu.jaxb; import java.util.ArrayList; import java.util.List; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "employees") public class Employees { @XmlElement(name = "employee", namespace=NameSpace.ADMIN_URI) private List<Employee> employees; public Employees() { employees = new ArrayList<Employee>(); } public List<Employee> getEmployees() { return employees; } public void addEmployee(Employee employee) { employees.add(employee); } }
package com.javaeye.terrencexu.jaxb; import javax.xml.bind.annotation.XmlElement; public class Employee { @XmlElement(name = "userId", namespace=NameSpace.ADMIN_URI) private String userId; @XmlElement(name = "password", namespace=NameSpace.ADMIN_URI) private String password; @XmlElement(name = "name", namespace=NameSpace.ADMIN_URI) private String name; @XmlElement(name = "age", namespace=NameSpace.ADMIN_URI) private int age; @XmlElement(name = "gender", namespace=NameSpace.ADMIN_URI) private String gender; public Employee() { } public Employee(String userId, String psw, String name, int age, Gender gender) { this.userId = userId; this.password = psw; this.name = name; this.age = age; this.gender = gender.getValue(); } public String getUserId() { return userId; } public String getPassword() { return password; } public String getName() { return name; } public int getAge() { return age; } public String getGender() { return gender; } }
package com.javaeye.terrencexu.jaxb; public class NameSpace { public static final String ADMIN_PREFIX = "admin"; public static final String ADMIN_URI = "http://www.company.com/management/employees/admin"; }
package com.javaeye.terrencexu.jaxb; public enum Gender { MALE("Male"), FEMALE("Female"); private String value; private Gender(String value) { this.value = value; } public String getValue() { return this.value; } }
3. 创建package-info.java和jaxb.index文件
创建package-info.java注册xmlns, 将该文件放在当前source code的package下面,如果不使用namespace可以不创建该文件
创建jaxb.index注册Employees, Employee,如果不使用包级context,可以不创建该文件
4. 创建XMLParser.java封装marshal/unmarshal操作
package com.javaeye.terrencexu.jaxb; import java.io.InputStream; import java.io.StringWriter; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; public final class XMLParser { private XMLParser() {} public static Object unmarshal(InputStream xml, Class<?> clazz) { Object obj = null; try { JAXBContext jc = JAXBContext.newInstance(clazz.getPackage().getName()); Unmarshaller u = jc.createUnmarshaller(); obj = u.unmarshal(xml); } catch (JAXBException e) { throw new RuntimeException("Can't unmarshal the XML file, error message: " + e.getMessage()); } return obj; } public static String marshal(Object obj, Class<?> clazz) { String result = null; try { JAXBContext jc = JAXBContext.newInstance(clazz.getPackage().getName()); Marshaller m = jc.createMarshaller(); StringWriter writer = new StringWriter(); m.marshal(obj, writer); result = writer.toString(); } catch (JAXBException e) { throw new RuntimeException("Can't marshal the XML file, error message: " + e.getMessage()); } return result; } }
5. 创建Test.java运行测试
package com.javaeye.terrencexu.jaxb; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.List; public class Test { public static void main(String[] args) throws FileNotFoundException { testUnmarshal(); testMarshal(); } public static void testUnmarshal() throws FileNotFoundException { Employees employees = (Employees) XMLParser.unmarshal( new FileInputStream(new File("F:\\workspace\\JavaStudy\\test\\employees.xml")), Employees.class); List<Employee> employeeList = employees.getEmployees(); if (employeeList != null && employeeList.size() > 0) { for (Employee employee : employeeList) { StringBuilder builder = new StringBuilder(); builder.append("[UserID: ").append(employee.getUserId()).append(", ") .append("Password: ").append(employee.getPassword()).append(", ") .append("Name: ").append(employee.getName()).append(", ") .append("Age: ").append(employee.getAge()).append(", ") .append("Gender").append(employee.getGender()).append("]"); System.out.println(builder.toString()); } } } public static void testMarshal() { Employees employees = new Employees(); employees.addEmployee(new Employee("[email protected]", "abc123_", "John Smith", 24, Gender.MALE)); employees.addEmployee(new Employee("[email protected]", "123456", "Christine Chen", 27, Gender.FEMALE)); String result = XMLParser.marshal(employees, Employees.class); System.out.println(result); } }
6. 运行结果: