简体   繁体   中英

Create Java object from xml file with xPath

I have an xml file in this format:

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

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

The @XBRead annotations define the XPath where to gather the elements.

And to extract the information you can call XMLBeam like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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