Employees.xml:

Java XML parsing tutorial example code


Employee.java


  1. package javaXMLParsing;
  2. public class Employee {
  3.     
  4.     String id;
  5.       String firstName;
  6.       String lastName;
  7.       String location;
  8.      
  9.       @Override
  10.       public String toString() {
  11.         return firstName+" "+lastName+"("+id+")"+location;
  12.       }
  13.  
  14. }

Java Example program to parse XML using DOM parser:

DOMParserDemo.java


  1. package javaXMLParsing;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4.  
  5. import javax.xml.parsers.DocumentBuilder;
  6. import javax.xml.parsers.DocumentBuilderFactory;
  7.  
  8. import org.w3c.dom.Document;
  9. import org.w3c.dom.Element;
  10. import org.w3c.dom.Node;
  11. import org.w3c.dom.NodeList;
  12.  
  13. public class DOMParserDemo {
  14.  
  15.  public static void main(String[] args) throws Exception {
  16.         //Create object of DOM Builder Factory  class By using
  17.        //DocumentBuilderFactory.newInstance() method

  18.         DocumentBuilderFactory factoryobj =
  19.             DocumentBuilderFactory.newInstance();
  20.      
  21.         //Create DOM Builder Object from DocumentBuilderFactory object
  22.         DocumentBuilder builder = factoryobj.newDocumentBuilder();
  23.      
  24.         //Load the XML document and parse it
  25.        
  26.         Document documentobj =
  27.           builder.parse(
  28.             ClassLoader.getSystemResourceAsStream("javaXMLParsing/Employee.xml"));
  29.      
  30.      
  31.         List<Employee> empList = new ArrayList<>();
  32.      
  33.         //Iterating through the nodes and extracting the data.
  34.         NodeList nodeList = documentobj.getDocumentElement().getChildNodes();
  35.      
  36.         for (int i = 0; i < nodeList.getLength(); i++) {
  37.      
  38.           //We have encountered an <employee> tag.
  39.  
  40.           Node node = nodeList.item(i);
  41.           if (node instanceof Element) {
  42.             Employee emp = new Employee();
  43.             emp.id = node.getAttributes().
  44.                 getNamedItem("id").getNodeValue();
  45.      
  46.    NodeList childNodes = node.getChildNodes();
  47.    for (int j = 0; j < childNodes.getLength(); j++) {
  48.        Node cNode = childNodes.item(j);
  49.      
  50.       //Identifying the child tag of employee every employee .
  51.       if (cNode instanceof Element) {
  52.        String content = cNode.getLastChild().
  53.                     getTextContent().trim();
  54.                 switch (cNode.getNodeName()) {
  55.                   case "firstName":
  56.                     emp.firstName = content;
  57.                     break;
  58.                   case "lastName":
  59.                     emp.lastName = content;
  60.                     break;
  61.                   case "location":
  62.                     emp.location = content;
  63.                     break;
  64.   }
  65.   }
  66.  }
  67.             empList.add(emp);
  68. }

  69. }
  70.      
  71.        //Printing the Employee list by using for each loop.
  72.  for (Employee emp : empList) {
  73.           System.out.println(emp);
  74.  }
  75.      
  76.  }
  77.  
  78. }



Output:


  1. saidesh kilaru(001)Bangalore 
  2. ajay chanukya(002)Chicago 
  3. Vinod Gandrakoti(003)Hyderabad

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu