I am reading an XML using dom4j by using XPath techniques for selecting desired nodes. Consider that my XML looks like this:
<Emp_Dir>
<Emp_Classification type ="Permanent" >
<Emp id= "1">
<name>jame</name>
<Emp_Bio>
<age>12</age>
<height>5.4</height>
<weight>78</weight>
</Emp_Bio>
<Empployment_History>
<job>
<salary>2000</salary>
<designation>senior developer</designation>
<duration>2years</duration>
</job>
<job>
<salary>1000</salary>
<designation>developer</designation>
<duration>3years</duration>
</job>
</Empployment_History>
</Emp>
.
.
.
</Emp_Classification>
<Emp_Classification type ="Contract" >
.
.
.
</Emp_Classification>
<Emp_Classification type ="PartTime" >
.
.
.
</Emp_Classification>
</Emp_Dir>
Note: The above XML might looks ugly to you but i only create this dummy file for the sake of understanding and keeping the secracy of my project
My desired goal is to get the Employment History of every permanent Employee, for this i managed to get all "Emp" nodes of permanent employees by using the following XPath Epression:
//Emp_Dir/Emp_Classification[@type='Permanent']/Emp
The code that gets and store the nodes looks like:
List<? extends Node> lstprmntEmps = document.selectNodes("//Emp_Dir/Emp_Classification/[@type='Permanent']/Emp");
ArrayList<Employee> Employees = new ArrayList<Employee>();//Employee is my custom class
for (Node node : lstprmntEmps)
{
Employees.add(ParseEmployee(node));//ParseEmployee(. . .) is my custom function that pareses emp XML and return Employee object
}
Now i want to ask following three question:
- How do i get the ID attribute of each Employee?
- How do i get the name element of each Employee?
- Last but the most important one, What XPath i need to specify in order to get the Job node of each employee? I tried follwoing but didn't succeded :(
node.selectNodes("/Emp/Empployment_History/job"); //this return zero nodes (or) node.selectNodes("//Emp/Empployment_History/job");// this return nodes more then expected
//Emp_Dir/Emp_Classification/[@type='Permanent']/Emp
doesn't seem right... i'd think it would be//Emp_Dir/Emp_Classification[@type='Permanent']/Emp
– MeBigFatGuy