使用 xPath 从 xml 文件创建 Java 对象 [英] Create Java object from xml file with xPath
问题描述
我有一个这种格式的 xml 文件:
I have an xml file in this format:
<Employee>
<EmployeeID>10</EmployeeID>
<Name>David</Name>
<Department>Service</Department>
</Employee>
我有一个很大的 xml 文件,其中仅包含这种格式的员工.我想要的是使用 xPath 从文件中选择节点,然后为每个条目创建一个 Java 对象:
I have a big xml file that consists only of employees in this format. What I want is to use xPath to select nodes from the file and then create a Java object for each entry:
public class Employee {
String name;
String department;
int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
}
这是我目前所拥有的.我似乎无法弄清楚如何最好地检索我需要的所有信息并创建对象:
Here is what I have so far. I can't seem to figure out how to best retrieve all the info I need, and create the objects:
public void parseFile(String fileName) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
Document document = null;
try {
builder = factory.newDocumentBuilder();
document = builder.parse(new File(fileName));
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xPath = xPathFactory.newXPath();
List<Employee> employees = getEmployeesFromXml(document, xPath);
} catch (SAXException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private List<Employee> getEmployeesFromXml(Document document, XPath xPath) {
List<Employee> list = new ArrayList<Employee>();
try {
// Create an expression that retrieves the values from xml
} catch (XPathExpressionException e) {
e.printStackTrace();
}
return null;
}
推荐答案
如果您不想解析 XML 并且对节点执行某些操作,只需将它们转换为 Java 对象,我建议您查看 XMLBeam.这是一个很好的框架,它加载您的 XML 文件并根据您为每个元素定义的 XPath 创建投影.
If you don't want to parse XML and do something with the nodes only convert them to Java objects I suggest to take a look at XMLBeam. It's a nice framework which loads your XML file and creates projections based on your XPath you define for each element.
例如,这是投影的样子:
For example this is how a projection looks like:
public interface ProfileData {
@XBRead("/root/personal_data/resultSet/firstname")
String getFirstName();
@XBRead("/root/personal_data/resultSet/surname")
String getSurname();
@XBRead("/root/personal_data/resultSet/core_areas")
String getCoreAreas();
interface Skill {
@XBRead("skillname")
String getName();
@XBRead("skillval")
String getExperience();
}
interface SkillSet {
@XBRead("../gencatname")
String getGenericCategory();
@XBRead("catname")
String getCategory();
@XBRead("skill")
List<Skill> getSkills();
}
@XBRead("/root/skills/tab/category")
List<SkillSet> getSkillSets();
}
@XBRead
注释定义 XPath 收集元素的位置.
The @XBRead
annotations define the XPath where to gather the elements.
为了提取信息,您可以像这样调用 XMLBeam:
And to extract the information you can call XMLBeam like this:
ProfileData profileData = new XBProjector().io().stream(yourXMLFileStream).read(ProfileData.class);
这篇关于使用 xPath 从 xml 文件创建 Java 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!