JAXB Marshaller
(编组器)示例
JAXB Marshaller
接口负责管理将 Java 内容树即 Java 对象转换为 XML 数据的过程。 可以将 XML 编组到各种输出目标。
1. JAXB 编组对象到 XML
1.1 创建编组器
通常,要创建编组器,可以重用此代码。
JAXBContext jaxbContext = JAXBContext.newInstance( Employee.class );
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
Employee employeeObj = new Employee(1, "Lokesh", "Gupta", new Department(101, "IT"));
//Overloaded methods to marshal to different outputs
jaxbMarshaller.marshal(employeeObj);
1.2 编组 XML 文件
OutputStream os = new FileOutputStream( "employee.xml" );
jaxbMarshaller.marshal( employeeObj, os );
1.3 编组至 SAX ContentHandler
假设MyContentHandler
是 org.xml.sax.ContentHandler
的实例。
jaxbMarshaller.marshal( employeeObj, new MyContentHandler() );
1.4 编组 DOM 文件
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.newDocument();
jaxbMarshaller.marshal( employeeObj, doc );
1.5 编组并打印到控制台
m.marshal( employeeObj, new PrintWriter( System.out ) );
2. 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
。
3.Marshaller
回调方法
您可以通过 JAXB 注解类中的自定义编组操作,例如 Employee.java
。 您需要定义两个方法,这些方法将在编组程序处理该类之前和之后进行监听。 在这些方法中,您可以执行诸如设置额外字段之类的操作。
package com.howtodoinjava.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");
}
}
4. JAXB 编组示例
将 Java 对象编组为 XML 字符串的示例。
package com.howtodoinjava.demo;
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import com.howtodoinjava.demo.model.Department;
import com.howtodoinjava.demo.model.Employee;
public class JaxbExample
{
public static void main(String[] args)
{
Employee employee = new Employee(1, "Lokesh", "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();
}
}
}
程序输出。
Before Marshaller Callback
After Marshaller Callback
<?xml version="1.0" encoding="UTF-8"?>
<employee>
<department>
<id>101</id>
<name>IT</name>
</department>
<firstName>Lokesh</firstName>
<id>1</id>
<lastName>Gupta</lastName>
</employee>
将我的问题放在评论部分。
学习愉快!