简体   繁体   English

使用xPath从xml文件创建Java对象

[英]Create Java object from xml file with xPath

I have an xml file in this format: 我有以下格式的xml文件:

<Employee>
    <EmployeeID>10</EmployeeID>
    <Name>David</Name>
    <Department>Service</Department>
</Employee>

I have a big xml file that consists only of employees in this format. 我有一个很大的xml文件,只包含这种格式的员工。 What I want is to use xPath to select nodes from the file and then create a Java object for each entry: 我想要的是使用xPath从文件中选择节点,然后为每个条目创建一个Java对象:

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;
}

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 . 如果您不想解析XML并且对节点执行某些操作,而仅将它们转换为Java对象,那么我建议看一下XMLBeam It's a nice framework which loads your XML file and creates projections based on your XPath you define for each element. 这是一个不错的框架,可以加载XML文件并根据为每个元素定义的XPath创建投影。

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();
}

The @XBRead annotations define the XPath where to gather the elements. @XBRead批注定义了XPath收集元素的位置。

And to extract the information you can call XMLBeam like this: 为了提取信息,您可以像这样调用XMLBeam:

ProfileData profileData = new XBProjector().io().stream(yourXMLFileStream).read(ProfileData.class);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM