java - 在 Java 中解析未格式化的 XML 并转换为对象
问题描述
我们有一个用 java 完成的应用程序。我们需要解析未格式化的 XML 并将其转换为 Java 对象。
在java中做这个的最好方法是什么?
谢谢
<category>
<pattern>WHAT IS MY NAME</pattern>
<template>
Your name is <get name="NAME" />.
<condition name="PHONE">
<li value="unknown">
<condition name="EMAIL">
<li value="unknown">I cannot find the contact details for the name.</li>
<li>You can email him/her at <get name="EMAIL" />.</li>
</condition>
</li>
<li>
<condition name="EMAIL">
<li value="unknown">You can call him/her on <get name="PHONE" />.</li>
<li>You can call them on <get name="PHONE" /> or email them at <get name="EMAIL" />.</li>
</condition>
</li>
</condition>
</template>
</category>
<category>
<pattern>WHO IS MY PARENT</pattern>
<template>
<templatetext>
Your parent is $get[PARENT_CONTACT_NAME].
You can email him/her at $get[PARENT_EMAIL].
His/her phone number is $get[PARENT_PHONE].
</templatetext>
</template>
</category>
解决方案
我认为对于如此复杂的 xml 结构,最好的解析方法是编写一个 xsd 模式,然后将您的 xml 转换为 java 对象。
下面的示例不会为您提供问题的确切解决方案,而只是使用 JAXB 将 xml 解析为 Java 对象的一个想法。显然,这个例子也不适用于如此复杂的 xml,但是一旦你为它编写了一个 xsd 模式,它可能会对你有所帮助:
这是employee.xml 文件:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee>
<fName>John</fName>
<id>1</id>
<lName>Paul</lName>
<domain>
<id>999</id>
<name>CS</name>
</domain>
</employee>
Employee.java 和 Domain.java :
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 fName;
private String lName;
private Domain domain;
public Employee() {
super();
}
public Employee(int id, String fName, String lName, Domain domain) {
super();
this.id = id;
this.fName = fName;
this.lName = lName;
this.domain = domain;
}
//Setters and Getters
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getfName() {
return fName;
}
public void setfName(String fName) {
this.fName = fName;
}
public String getlName() {
return lName;
}
public void setlName(String lName) {
this.lName = lName;
}
public Domain getDomain() {
return domain;
}
public void setDomain(Domain domain) {
this.domain = domain;
}
@Override
public String toString() {
return "Employee [id=" + id + ", firstName=" + fName + ",lastName=" + lName +
", department="+ domain + "]";
}
}
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;
@XmlRootElement(name = "domain")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Domain implements Serializable {
private static final long serialVersionUID = 1L;
Integer id;
String name;
public Domain() {
super();
}
public Domain(Integer id, String name) {
super();
this.id = id;
this.name = name;
}
//Setters and Getters
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Domain [id=" + id + ", name=" + name + "]";
}
}
JAXB 将 XML 文件解析为 Java:
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.File;
public class ParseXMLFile {
public static void main(String[] args) {
File xmlFile = new File("src/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();
}
}
}
输出:
Employee [id=1, firstName=John,lastName=Paul, department=Domain [id=999, name=CS]]
希望你对这些事情有所了解。
推荐阅读
- excel - 如何复制一组列并将它们放在VBA中的一组行中
- wordpress - 使用 WooCommerce Rest API 创建带有优惠券行的订单
- azure - terraform:仅将公共 IP 添加到一个 azure VM
- python - 如何使用多个 3D 数组训练回归模型?
- javascript - 根据 JavaScript 中的值设置 CSS 样式
- leaflet - 带有翻转坐标的传单 .png 图像地图?
- android - Drawable 在 Android Studio 的不同活动布局中表现不同
- python - 从 Mixcloud 播放列表 Python Selenium 中提取 URL
- python - 我怎样才能让我的代码重复一个动作?我使用了“for i in range(x):”,但它似乎不起作用
- poker - 有多少种不同的方法可以将一组扑克分成 6 组?解决这个问题的最简单方法是什么?