Parsing a XML file in Java using DOM Parser without recursion

Let us consider the below Employee XML that represents the Employee details with Salary

     <EmpDetails>
         <Employee>
             <Name>ABC</Name>
             <Designation>ABC</Designation>
             <Scale>50000-200000</Scale>
             <Salary>
                   <Basic>121199.00</Basic>
                   <HRA>20000.00</HRA>
                   <TA>10000.00</TA>
              </Salary>
           </Employee>
           <Employee>
              <Name>XYZ</Name>
              <Designation>ABC</Designation>
              <Scale>12100-100000</Scale>
              <Salary>
                    <Basic>21199.00</Basic>
                    <HRA>2000.00</HRA>
                    <TA>1000.00</TA>
              </Salary>
           </Employee>
       </EmpDetails>
 

Now let us parse the above Employees XML and calculate the Total salary of each employee without using recursion .

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.*;
import org.w3c.dom.*;

public class employees_DOM {
   public static void main(String[] args) {   

      try{
     File employees = new File("emp.xml");
     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
     DocumentBuilder db = dbf.newDocumentBuilder();
     Document doc = db.parse(employees);
     doc.getDocumentElement().normalize();

         // get all the employee nodes
       NodeList employeeNodeList = doc.getElementsByTagName("Employee");

      for(int i=0; i<employeeNodeList.getLength(); i++) {
        double total_salary=0.0;
        Node employeeNode = employeeNodeList.item(i); // one employee node at a time
        NodeList employeeChildNodeList = employeeNode.getChildNodes();    // all child nodes of employee node

         for(int j=0; j<employeeChildNodeList.getLength(); j++) {
              Node employeeChildNode = employeeChildNodeList.item(j);   //Name , Designation , Scale ,Salary ....

              if (employeeChildNode.getNodeType() == Node.ELEMENT_NODE) {
                    String employeeChildNodeName=employeeChildNode.getNodeName();
                    Node employeeTextNode = employeeChildNode.getFirstChild();//the only text node
                    int no_of_childs=employeeChildNode.getChildNodes().getLength(); // no of childs inside name  child , designation , ..., salary  . salary has more than one child nodes.
                  if (no_of_childs==1)
                         System.out.println( employeeChildNodeName + "  = " +employeeTextNode.getNodeValue().trim());
                  else
                    {   // samething for salary  node

                    NodeList salaryChildNodeList = employeeChildNode.getChildNodes();   // all child nodes of salary node
                      for(int k=0; k<salaryChildNodeList.getLength(); k++) {
                         Node salaryChildNode = salaryChildNodeList.item(k); //  to get  basic , hra, ta ...

                         if (salaryChildNode.getNodeType() == Node.ELEMENT_NODE) {
                             String salaryChildNodeName=salaryChildNode.getNodeName();
                             Node salaryTextNode = salaryChildNode.getFirstChild(); //the only text node
                             System.out.println( salaryChildNodeName + "  = " + salaryTextNode.getNodeValue().trim());
                             total_salary=total_salary + Double.parseDouble(salaryTextNode.getNodeValue().trim());
                                      }
                           }
                     }

                  }

             }
    System.out.println("Total Salary = " + total_salary + "n");
           }

      }  catch (Exception e) {e.printStackTrace(System.err);}

    }
} 


Output:

You may also like

Leave a Reply