Java解析XMl

来源:互联网 发布:王诺诺知乎live 编辑:程序博客网 时间:2024/05/18 13:31

在java开发中使用XML几乎是每个程序员都会碰到的,目前可供我们选择的库也有很多,包括,dom4j, jaxen, SAX等等,本文将介绍如何使用JAXB进行Object-XML mapping.

JAXB目前已经集成到了JDK6中,但是还是推荐下载使用最新版的jaxb库, https://jaxb.dev.java.net/

从JAXB2.0开始,可以通过使用annotation进行Object-XMl mapping。

本文将通过以下步骤演示如何使用JAXB annotation绑定使用了namespace的XML文件

  • 创建employees.xml文件
  • 创建Employees.java和Employee.java绑定employees.xml
  • 创建package-info.java和jaxb.index文件
  • 创建XMLParser.java封装marshal/unmarshal操作
  • 创建Test.java运行测试

现在将逐步展开演示

创建employees.xml文件

<?xml version="1.0" encoding="utf-8" ?>  <employees xmlns:admin="http://www.company.com/management/employees/admin">      <admin:employee>          <admin:userId>johnsmith@company.com</admin:userId>          <admin:password>abc123_</admin:password>          <admin:name>John Smith</admin:name>          <admin:age>24</admin:age>          <admin:gender>Male</admin:gender>      </admin:employee>      <admin:employee>          <admin:userId>christinechen@company.com</admin:userId>          <admin:password>123456</admin:password>          <admin:name>Christine Chen</admin:name>          <admin:age>27</admin:age>          <admin:gender>Female</admin:gender>      </admin:employee>  </employees>  

这是一个很普通的xml文件,用于展示组织内部的成员(employee)状况,这里有一点需要注意的是使用了namespace,这也是本文将要演示的重点。

创建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;      }  }  

创建package-info.java和jaxb.index文件

创建package-info.java注册xmlns, 将该文件放在当前source code的package下面,如果不使用namespace可以不创建该文件

@XmlSchema(  xmlns={          @XmlNs(prefix=NameSpace.ADMIN_PREFIX, namespaceURI=NameSpace.ADMIN_URI)  }  )  package com.javaeye.terrencexu.jaxb;  import javax.xml.bind.annotation.XmlNs;  import javax.xml.bind.annotation.XmlSchema;  

创建jaxb.index注册Employees, Employee,如果不使用包级context,可以不创建该文件

Employees  Employee

创建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;      }  }  

创建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("johnsmith@company.com", "abc123_", "John Smith", 24, Gender.MALE));          employees.addEmployee(new Employee("christinechen@company.com", "123456", "Christine Chen", 27, Gender.FEMALE));          String result = XMLParser.marshal(employees, Employees.class);          System.out.println(result);      }  }  

运行结果:

[UserID: johnsmith@company.com, Password: abc123_, Name: John Smith, Age: 24, GenderMale]  [UserID: christinechen@company.com, Password: 123456, Name: Christine Chen, Age: 27, GenderFemale]  <?xml version="1.0" encoding="UTF-8" standalone="yes"?><employees xmlns:admin="http://www.company.com/management/employees/admin"><admin:employee><admin:userId>johnsmith@company.com</admin:userId><admin:password>abc123_</admin:password><admin:name>John Smith</admin:name><admin:age>24</admin:age><admin:gender>Male</admin:gender></admin:employee><admin:employee><admin:userId>christinechen@company.com</admin:userId><admin:password>123456</admin:password><admin:name>Christine Chen</admin:name><admin:age>27</admin:age><admin:gender>Female</admin:gender></admin:employee></employees>  
0 0
原创粉丝点击