原文: https://howtodoinjava.com/jaxb/jaxb-exmaple-marshalling-and-unmarshalling-list-or-set-of-objects/

我们知道 JAXB(用于 XML 绑定的 Java 架构)允许 Java 开发人员将 Java 类映射到 XML 表示形式。 JAXB 提供了两个主要功能:将 Java 对象编组为 XML 的能力和相反的功能,即将 XML 解组为 Java 对象的能力。 JAXB 主要用于实现 Web 服务或任何其他此类客户端接口的应用,在该应用中,数据需要以 XML 格式(而不是 HTML 格式)传输,而对于可视客户端(如 Web 浏览器)来说,HTML 格式是默认格式。

一个很好的例子是 facebook API。 Facebook 已通过 RESTful Web 服务的形式通过一些开放的端点公开了其服务,您在其中单击 URL 并发布了一些参数,而 API 以 xml 格式返回数据。 现在由您决定如何使用这些数据。

在本文中,我以编组和解组对象的集合为例。 Java 中的这些集合可以是:ListSet实现,例如 ArrayListHashSet

1)JAXB Maven 依赖关系

要运行 JAXB 示例,我们需要添加运行时 maven 依赖项,如下所示:

  1. <dependency>
  2. <groupId>com.sun.xml.bind</groupId>
  3. <artifactId>jaxb-core</artifactId>
  4. <version>2.2.8-b01</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.sun.xml.bind</groupId>
  8. <artifactId>jaxb-impl</artifactId>
  9. <version>2.2-promoted-b65</version>
  10. </dependency>

2)模型类

我创建了一个模型类“Employee”,它具有一些公开字段。 我想构建可以解析一组employees的代码。 请注意,JAXB 在最重要的类上需要 @XmlRootElement 注解,我们将对其进行编组或解组。

ArrayList类是集合框架的一部分,它没有任何 JAXB 注解。 因此,我们需要另外一个类别“Employees”,该类别将代表一组雇员。 现在,在该类中,我们可以添加任何我们喜欢的注解。

  1. @XmlRootElement(name = "employee")
  2. @XmlAccessorType (XmlAccessType.FIELD)
  3. public class Employee
  4. {
  5. private Integer id;
  6. private String firstName;
  7. private String lastName;
  8. private double income;
  9. //Getters and Setters
  10. }
  1. import java.util.List;
  2. import javax.xml.bind.annotation.XmlAccessType;
  3. import javax.xml.bind.annotation.XmlAccessorType;
  4. import javax.xml.bind.annotation.XmlElement;
  5. import javax.xml.bind.annotation.XmlRootElement;
  6. @XmlRootElement(name = "employees")
  7. @XmlAccessorType (XmlAccessType.FIELD)
  8. public class Employees
  9. {
  10. @XmlElement(name = "employee")
  11. private List<Employee> employees = null;
  12. public List<Employee> getEmployees() {
  13. return employees;
  14. }
  15. public void setEmployees(List<Employee> employees) {
  16. this.employees = employees;
  17. }
  18. }

3)将列表编组为 XML 的示例

编组是“将 Java 对象转换为 xml 表示形式”。 在下面的示例代码中,我首先在控制台中写入employees列表,然后在文件中写入。

  1. //Initialize the employees list
  2. static Employees employees = new Employees();
  3. static
  4. {
  5. employees.setEmployees(new ArrayList<Employee>());
  6. //Create two employees
  7. Employee emp1 = new Employee();
  8. emp1.setId(1);
  9. emp1.setFirstName("Lokesh");
  10. emp1.setLastName("Gupta");
  11. emp1.setIncome(100.0);
  12. Employee emp2 = new Employee();
  13. emp2.setId(2);
  14. emp2.setFirstName("John");
  15. emp2.setLastName("Mclane");
  16. emp2.setIncome(200.0);
  17. //Add the employees in list
  18. employees.getEmployees().add(emp1);
  19. employees.getEmployees().add(emp2);
  20. }
  21. private static void marshalingExample() throws JAXBException
  22. {
  23. JAXBContext jaxbContext = JAXBContext.newInstance(Employees.class);
  24. Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
  25. jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
  26. //Marshal the employees list in console
  27. jaxbMarshaller.marshal(employees, System.out);
  28. //Marshal the employees list in file
  29. jaxbMarshaller.marshal(employees, new File("c:/temp/employees.xml"));
  30. }

上面代码的输出是:

JAXB – 编组和解组对象列表或集合 - 图1

JAXB 编组示例输出

4)解组 XML 到列表的示例

解组是将 xml 转换回 Java 对象的过程。 让我们看一下Employees类的示例。

  1. private static void unMarshalingExample() throws JAXBException
  2. {
  3. JAXBContext jaxbContext = JAXBContext.newInstance(Employees.class);
  4. Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
  5. //We had written this file in marshalling example
  6. Employees emps = (Employees) jaxbUnmarshaller.unmarshal( new File("c:/temp/employees.xml") );
  7. for(Employee emp : emps.getEmployees())
  8. {
  9. System.out.println(emp.getId());
  10. System.out.println(emp.getFirstName());
  11. }
  12. }
  13. Output:
  14. 1
  15. Lokesh
  16. 2
  17. John

5. 源代码下载

要下载以上示例的源代码,请点击以下链接。

下载源码

学习愉快!