0
votes

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:

  1. How do i get the ID attribute of each Employee?
  2. How do i get the name element of each Employee?
  3. 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

1
this xpath //Emp_Dir/Emp_Classification/[@type='Permanent']/Emp doesn't seem right... i'd think it would be //Emp_Dir/Emp_Classification[@type='Permanent']/EmpMeBigFatGuy
@MeBigFatGuy : yes you are right, i fix that. good catch thxJame

1 Answers

1
votes

If I'm understanding correctly, when you say "the Job node of each employee", you are not looking for an XPath that returns all the job nodes in the XML file -- rather, you are looking to implement the ParseEmployee method, and pulling out of the document the list of jobs for one given employee (identified by the XML node passed in)?

It could be implemented something like this (omitting the part where you actually do something useful with the extracted strings, and omitting the null checks and such that you'll want to include!)

public parseEmployee (Element e) {
   String id = e.attributeValue("id");
   String name = e.elements("name").get(0).getText();
   List<Element> jobs = e.elements("job");
   for (Element job : jobs) {
       parseJob(job);
   }
}

parseJob() will operate on similar principles and pull the relevant information out of the <job> element passed to it.