Create Java object from xml file with xPath
<p>I have an xml file in this format: </p>

<pre><code>&lt;Employee&gt;
    &lt;EmployeeID&gt;10&lt;/EmployeeID&gt;
    &lt;Name&gt;David&lt;/Name&gt;
    &lt;Department&gt;Service&lt;/Department&gt;
&lt;/Employee&gt;
</code></pre>

<p>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: </p>

<pre><code>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; 
}
</code></pre>

<p>}</p>

<p>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: </p>

<pre><code>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&lt;Employee&gt; employees = getEmployeesFromXml(document, xPath); 

    } catch (SAXException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private List&lt;Employee&gt; getEmployeesFromXml(Document document, XPath xPath) {

    List&lt;Employee&gt; list = new ArrayList&lt;Employee&gt;();

    try {
        // Create an expression that retrieves the values from xml 
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }

    return null;
}
</code></pre>