如何将 XML 解组为对象
创建解组器
通常,要创建 Unmarshaller,我们可以重用此代码。
JAXBContext jaxbContext = JAXBContext.newInstance( Employee.class ); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); //Overloaded methods to unmarshal from different xml sources Employee employee = (Employee) jaxbUnmarshaller.unmarshal( xmlSource );
从 InputStream 解组
InputStream inStream = new FileInputStream( "employee.xml" ); Employee employee = (Employee) jaxbUnmarshaller.unmarshal( inStream );
从 URL 解组
URL url = new URL( "http://localhost:8080/employee.xml" ); Employee employee = (Employee) jaxbUnmarshaller.unmarshal( url );
解组字符串内容
String xmlString = "..."; Employee employee = (Employee) jaxbUnmarshaller.unmarshal(new StringReader(xmlString));
从 org.w3c.dom.Node 解组
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder db = dbf.newDocumentBuilder(); Document document = db.parse(new File( "employee.xml")); //Node Employee employee = (Employee) jaxbUnmarshaller.unmarshal( document );
JAXB Unmarshaller接口负责管理将 XML 数据反序列化为 Java 对象的过程。
可以对各种输入源进行对象的解组。
JAXB 解组器示例
测试xml文件 employee.xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee>
<department>
<id>101</id>
<name>IT</name>
</department>
<firstName>JackLi</firstName>
<id>1</id>
<lastName>Gupta</lastName>
</employee>
将 XML 文件解组为 Java Object
package com.onitroad.demo;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import com.onitroad.demo.model.Employee;
public class JaxbExample
{
public static void main(String[] args)
{
String fileName = "employee.xml";
jaxbXmlFileToObject(fileName);
}
private static void jaxbXmlFileToObject(String fileName) {
File xmlFile = new File(fileName);
JAXBContext jaxbContext;
try
{
jaxbContext = JAXBContext.newInstance(Employee.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Employee employee = (Employee) jaxbUnmarshaller.unmarshal(xmlFile);
System.out.println(employee);
}
catch (JAXBException e)
{
e.printStackTrace();
}
}
}
JAXB 解组器属性
当前,Unmarshaller 上的所有 JAXB 提供程序都不需要支持任何属性。
但是,某些提供程序可能支持他们自己的一组特定于提供程序的属性。
更多: zhilu jiaocheng
解组器事件回调
我们可以通过在 JAXB 注释类中添加这些回调方法来自定义解组操作,例如
Employee.java。
我们需要定义两个方法,它们将在 Unmarshaller处理该类之前和之后进行侦听。
void beforeUnmarshal(Unmarshaller unmarshaller, Object parent) {}void afterUnmarshal(Unmarshaller unmarshaller, Object parent) {}
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 + "]";
}
// It is called immediately after the object is created and before the unmarshalling begins.
// The callback provides an opportunity to initialize JavaBean properties prior to unmarshalling.
void beforeUnmarshal(Unmarshaller unmarshaller, Object parent) {
System.out.println("Before Unmarshaller Callback");
}
// It is called after all the properties are unmarshalled for this object,
// but before this object is set to the parent object.
void afterUnmarshal(Unmarshaller unmarshaller, Object parent) {
System.out.println("After Unmarshaller Callback");
}
}
日期:2020-09-17 00:09:40 来源:oir作者:oir
