JAXB 将对象编组到 XML
创建 Marshaller
通常,要创建编组器,我们可以重用此代码。
JAXBContext jaxbContext = JAXBContext.newInstance( Employee.class ); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); Employee employeeObj = new Employee(1, "JackLi", "Gupta", new Department(101, "IT")); //Overloaded methods to marshal to different outputs jaxbMarshaller.marshal(employeeObj);
编组到 XML 文件
OutputStream os = new FileOutputStream( "employee.xml" ); jaxbMarshaller.marshal( employeeObj, os );
编组到 SAX ContentHandler
假设MyContentHandler
是org.xml.sax.ContentHandler
的实例。
jaxbMarshaller.marshal( employeeObj, new MyContentHandler() );
元组到 DOM 文档
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.newDocument(); jaxbMarshaller.marshal( employeeObj, doc );
编组并打印到控制台
m.marshal( employeeObj, new PrintWriter( System.out ) );
JAXB Marshaller 属性
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); //or jaxbMarshaller.setProperty("jaxb.formatted.output", Boolean.TRUE);
所有 JAXB 提供程序都需要支持以下属性集。
某些提供程序可能支持其他属性。
- jaxb.encoding - 编组 XML 数据时使用的输出编码。如果未指定此属性,
Marshaller
将默认使用UTF-8
。 - jaxb.formatted.output - 值可以是
true
或者false
。Marshaller
是否会使用换行符和缩进来格式化生成的 XML 数据。默认值为false
。 - jaxb.schemaLocation - 它允许客户端应用程序在生成的 XML 数据中指定一个
xsi:schemaLocation
属性。 - jaxb.noNamespaceSchemaLocation - 它允许客户端应用程序在生成的 XML 数据中指定一个
xsi:noNamespaceSchemaLocation
属性。 - jaxb.fragment - 确定 Marshaller 是否会生成文档级事件。值可以是
true
或者false
。
https://onitroad.com 更多教程
Marshaller 回调方法
我们可以在 JAXB 注释类中自定义编组操作,例如 Employee.java
。
我们需要定义两个方法,它们将在编组器处理该类之前和之后进行侦听。
在这些方法中,我们可以执行诸如设置另外字段之类的操作
package com.onitroad.demo.model; import java.io.Serializable; import javax.xml.bind.Marshaller; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "employee") @XmlAccessorType(XmlAccessType.PROPERTY) public class Employee implements Serializable { private static final long serialVersionUID = 1L; private Integer id; private String firstName; private String lastName; private Department department; public Employee() { super(); } //Setters and Getters @Override public String toString() { return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", department=" + department + "]"; } // Invoked by Marshaller after it has created an instance of this object. boolean beforeMarshal(Marshaller marshaller) { System.out.println("Before Marshaller Callback"); return true; } // Invoked by Marshaller after it has marshalled all properties of this object. void afterMarshal(Marshaller marshaller) { System.out.println("After Marshaller Callback"); } }
JAXB Marshaller
接口负责管理将 Java 内容树序列化的过程,例如:Java 对象到 XML 数据。
这种对 XML 的编组可以对各种输出目标进行。
JAXB 编组示例
将 Java Object 编组为 XML 字符串
package com.onitroad.demo; import java.io.StringWriter; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import com.onitroad.demo.model.Department; import com.onitroad.demo.model.Employee; public class JaxbExample { public static void main(String[] args) { Employee employee = new Employee(1, "JackLi", "Gupta", new Department(101, "IT")); jaxbObjectToXML(employee); } private static void jaxbObjectToXML(Employee employee) { try { JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); // To format XML //Print XML String to Console jaxbMarshaller.marshal(employee, System.out); } catch (JAXBException e) { e.printStackTrace(); } } }
日期:2020-09-17 00:09:43 来源:oir作者:oir