将 XML 文件转换为 Java 对象
从文件中读取 XML 与上面的示例非常相似。
你只需要传递 File
对象来代替 StringReader
对象。File
将引用要在文件系统中读取的 '.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>
File xmlFile = new File("employee.xml"); 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(); }
import java.io.Serializable; 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(); } public Employee(int id, String fName, String lName, Department department) { super(); this.id = id; this.firstName = fName; this.lastName = lName; this.department = department; } //Setters and Getters @Override public String toString() { return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", department="+ department + "]"; } } @XmlRootElement(name = "department") @XmlAccessorType(XmlAccessType.PROPERTY) public class Department implements Serializable { private static final long serialVersionUID = 1L; Integer id; String name; public Department() { super(); } public Department(Integer id, String name) { super(); this.id = id; this.name = name; } //Setters and Getters @Override public String toString() { return "Department [id=" + id + ", name=" + name + "]"; } }
使用 JAXB vs SAX vs DOM
Java 提供了许多方法来读取 XML 文件并使用 XL 内容打印、在应用程序中使用或者填充 Java 对象中的数据以进一步在应用程序生命周期中使用。
用于此目的的三个主要 API 是用于 XML 的简单 API(SAX
)、文档对象模型(DOM
)和用于 XML 绑定的 Java 架构(JAXB
)。
SAX
或者DOM
解析器使用 JAXP API 来解析 XML 文档。
两者都扫描文档并在逻辑上将其分解为离散的部分(例如节点、文本和注释等)。
SAX
解析器从文档的开头开始,并按照它找到的顺序将文档的每一部分传递给应用程序。没有任何内容保存在内存中,因此它无法进行任何内存操作。DOM
解析器创建一个对象树,表示内存中Document
对象中数据的内容和组织。在这里,应用程序可以在树中导航以访问它需要的数据,并在适当的情况下对其进行操作。- 而
JAXB
将文档解组为 Java 内容对象。Java 内容对象表示 XML 文档的内容和组织,可直接用于程序。
www. On IT Road .com
将 XML 字符串转换为 Java 对象
要读取 XML,首先要获取 JAXBContext
。
它是 JAXB API 的入口点,并提供解组、编组和验证操作的方法。
现在从 JAXBContext
获取 Unmarshaller
实例。
它是 unmarshal()
方法从指定的 XML 解组 XML 数据并返回结果内容树。
String xmlString = "<employee>" + " <department>" + " <id>101</id>" + " <name>IT</name>" + " </department>" + " <firstName>JackLi</firstName>" + " <id>1</id>" + " <lastName>Gupta</lastName>" + "</employee>"; JAXBContext jaxbContext; try { jaxbContext = JAXBContext.newInstance(Employee.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); Employee employee = (Employee) jaxbUnmarshaller.unmarshal(new StringReader(xmlString)); System.out.println(employee); } catch (JAXBException e) { e.printStackTrace(); }
日期:2020-09-17 00:09:44 来源:oir作者:oir