原文: https://howtodoinjava.com/jaxb/jaxb-unmarshaller-example/

JAXB Unmarshaller接口负责管理将 XML 数据反序列化为 Java 对象的过程。 可以将对象解组到各种输入源。

1.如何将 XML 解组到对象

1.1 创建解组器

通常,要创建Unmarshaller,可以重用此代码。

  1. JAXBContext jaxbContext = JAXBContext.newInstance( Employee.class );
  2. Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
  3. //Overloaded methods to unmarshal from different xml sources
  4. Employee employee = (Employee) jaxbUnmarshaller.unmarshal( xmlSource );

1.2 从InputStream解组

  1. InputStream inStream = new FileInputStream( "employee.xml" );
  2. Employee employee = (Employee) jaxbUnmarshaller.unmarshal( inStream );

1.3 从 URL 解组

  1. URL url = new URL( "http://localhost:8080/employee.xml" );
  2. Employee employee = (Employee) jaxbUnmarshaller.unmarshal( url );

1.4 解组字符串内容

  1. String xmlString = "...";
  2. Employee employee = (Employee) jaxbUnmarshaller.unmarshal(new StringReader(xmlString));

1.5 从org.w3c.dom.Node解组

  1. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  2. dbf.setNamespaceAware(true);
  3. DocumentBuilder db = dbf.newDocumentBuilder();
  4. Document document = db.parse(new File( "employee.xml")); //Node
  5. Employee employee = (Employee) jaxbUnmarshaller.unmarshal( document );

2. JAXB 解组器属性

当前,Unmarshaller上的所有 JAXB 供应器都不需要所有属性。 但是,某些供应器可能支持它们自己的一组供应器特定属性。

3.解组事件回调

您可以通过在 JAXB 注解的类中添加这些回调方法来自定义解组操作,例如Employee.java。 您需要定义两个方法,这些方法将在Unmarshaller处理该类之前和之后监听。

  • void beforeUnmarshal(Unmarshaller unmarshaller, Object parent) {}
  • void afterUnmarshal(Unmarshaller unmarshaller, Object parent) {}
  1. package com.howtodoinjava.demo.model;
  2. import java.io.Serializable;
  3. import javax.xml.bind.Marshaller;
  4. import javax.xml.bind.annotation.XmlAccessType;
  5. import javax.xml.bind.annotation.XmlAccessorType;
  6. import javax.xml.bind.annotation.XmlRootElement;
  7. @XmlRootElement(name = "employee")
  8. @XmlAccessorType(XmlAccessType.PROPERTY)
  9. public class Employee implements Serializable {
  10. private static final long serialVersionUID = 1L;
  11. private Integer id;
  12. private String firstName;
  13. private String lastName;
  14. private Department department;
  15. public Employee() {
  16. super();
  17. }
  18. //Setters and Getters
  19. @Override
  20. public String toString() {
  21. return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", department="
  22. + department + "]";
  23. }
  24. // It is called immediately after the object is created and before the unmarshalling begins.
  25. // The callback provides an opportunity to initialize JavaBean properties prior to unmarshalling.
  26. void beforeUnmarshal(Unmarshaller unmarshaller, Object parent) {
  27. System.out.println("Before Unmarshaller Callback");
  28. }
  29. // It is called after all the properties are unmarshalled for this object,
  30. // but before this object is set to the parent object.
  31. void afterUnmarshal(Unmarshaller unmarshaller, Object parent) {
  32. System.out.println("After Unmarshaller Callback");
  33. }
  34. }

4. JAXB 解组器示例

将 XML 文件解组到 Java 对象的示例。

  1. package com.howtodoinjava.demo;
  2. import java.io.File;
  3. import javax.xml.bind.JAXBContext;
  4. import javax.xml.bind.JAXBException;
  5. import javax.xml.bind.Unmarshaller;
  6. import com.howtodoinjava.demo.model.Employee;
  7. public class JaxbExample
  8. {
  9. public static void main(String[] args)
  10. {
  11. String fileName = "employee.xml";
  12. jaxbXmlFileToObject(fileName);
  13. }
  14. private static void jaxbXmlFileToObject(String fileName) {
  15. File xmlFile = new File(fileName);
  16. JAXBContext jaxbContext;
  17. try
  18. {
  19. jaxbContext = JAXBContext.newInstance(Employee.class);
  20. Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
  21. Employee employee = (Employee) jaxbUnmarshaller.unmarshal(xmlFile);
  22. System.out.println(employee);
  23. }
  24. catch (JAXBException e)
  25. {
  26. e.printStackTrace();
  27. }
  28. }
  29. }

程序输出。

  1. Before Unmarshaller Callback
  2. After Unmarshaller Callback
  3. Employee [id=1, firstName=Lokesh, lastName=Gupta, department=Department [id=101, name=IT]]

其中employee.xml文件是:

  1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  2. <employee>
  3. <department>
  4. <id>101</id>
  5. <name>IT</name>
  6. </department>
  7. <firstName>Lokesh</firstName>
  8. <id>1</id>
  9. <lastName>Gupta</lastName>
  10. </employee>

向我提出您的有关使用 JAXB 注解在 Java 中解组的问题。

学习愉快!