How to do search in XML file using DOM Parser in Java?
Problem Description:
<Employees>
<Employee id="1">
<name>abc</name>
<age>23</age>
</Employee >
<Employee id="2">
<name>xyz</name>
<age>29</age>
</Employee>
<Employee id="3">
<name>lmn</name>
<age>34</age>
</Employee>
<name>abc</name>
<age>23</age>
</Employee >
<Employee id="2">
<name>xyz</name>
<age>29</age>
</Employee>
<Employee id="3">
<name>lmn</name>
<age>34</age>
</Employee>
</Employees>
Here requirement is to search child node Employee whose id is 2. Similarly user can search any child node.
Here requirement is to search child node Employee whose id is 2. Similarly user can search any child node.
Steps:
1) Parse XML file or Create an instance of Document interface present in org.w3c.dom.* package.
For Creating an instance of Document interface we need to use DocumentBuilderFactory, DocumentBuilder classes present in javax.xml.parsers.* package and File class present in java.io.* package.File file = new File(XML_file_location);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(file);
2) Get all the child nodes Employee of root node Employees. It returns the instance of NodeList interface present in org.w3c.dom.* package.
NodeList list = document.getElementsByTagName("Employee");
3) Now one by one examine each child node Employee.
pom.xml
Employee.java
SearchXML.java
Download Project
Click here for more questions on DOM Parser.
https://docs.oracle.com/javase/7/docs/api/javax/xml/parsers/DocumentBuilderFactory.html
https://docs.oracle.com/javase/7/docs/api/javax/xml/parsers/DocumentBuilder.html
https://docs.oracle.com/javase/7/docs/api/org/w3c/dom/Document.html
https://docs.oracle.com/javase/7/docs/api/org/w3c/dom/NodeList.html
https://docs.oracle.com/javase/7/docs/api/org/w3c/dom/Node.html
https://docs.oracle.com/javase/7/docs/api/org/w3c/dom/Element.html
Thank you friends, I hope you have clearly understood the solution of this problem. If you have any doubt, suggestion or query please feel free to comment below. You can also discuss this solution in our forum.
![]() |
Figure 1 |
2) Get all the child nodes Employee of root node Employees. It returns the instance of NodeList interface present in org.w3c.dom.* package.
NodeList list = document.getElementsByTagName("Employee");
3) Now one by one examine each child node Employee.
- From all the child nodes that we got in Step 3 take one child node. It returns instance of Node interface present in org.w3c.dom.* package.
- Check whether this node is Element node or not?
- If it is an Element node then typecast it and get its data (attributes and properties).
- Get its id as follows.
- Compare this id with id of Employee that you want to search.
if (String.valueOf(id).equals(element.getAttribute("id"))) {}
- Now get all employee properties as follows.
Software Required:
- Java
- Eclipse
- Maven
Note : I suggest to use latest version of all the software. If you need any help regarding these software installation please comment below I will help you.
Project Structure:
Java Code:
employee.xml
12345678910111213141516<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Employees>
<Employee id="1">
<name>Rohit</name>
<gender>male</gender>
<age>25</age>
<role>Programmer</role>
</Employee>
<Employee id="2">
<name>Bhavna</name>
<gender>female</gender>
<age>20</age>
<role>Student</role>
</Employee>
</Employees>
pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.javamultiplex</groupId> | |
<artifactId>DOM</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<build> | |
<pluginManagement> | |
<plugins> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<version>3.6.1</version> | |
<configuration> | |
<source>1.8</source> | |
<target>1.8</target> | |
</configuration> | |
</plugin> | |
</plugins> | |
</pluginManagement> | |
</build> | |
</project> |
Employee.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javamultiplex; | |
public class Employee { | |
private int id; | |
private String name; | |
private int age; | |
private String gender; | |
private String role; | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public int getAge() { | |
return age; | |
} | |
public void setAge(int age) { | |
this.age = age; | |
} | |
public String getGender() { | |
return gender; | |
} | |
public void setGender(String gender) { | |
this.gender = gender; | |
} | |
public String getRole() { | |
return role; | |
} | |
public void setRole(String role) { | |
this.role = role; | |
} | |
@Override | |
public String toString() { | |
return "Employee[id="+getId()+", name="+getName()+", gender="+getGender()+", age="+getAge()+", role="+getRole()+"]"; | |
} | |
public int getId() { | |
return id; | |
} | |
public void setId(int id) { | |
this.id = id; | |
} | |
} |
SearchXML.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javamultiplex; | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.Scanner; | |
import javax.xml.parsers.DocumentBuilder; | |
import javax.xml.parsers.DocumentBuilderFactory; | |
import javax.xml.parsers.ParserConfigurationException; | |
import org.w3c.dom.Document; | |
import org.w3c.dom.Element; | |
import org.w3c.dom.Node; | |
import org.w3c.dom.NodeList; | |
import org.xml.sax.SAXException; | |
public class SearchXML { | |
public static void main(String[] args) throws ParserConfigurationException,SAXException, IOException { | |
File file = new File("src/main/resources/employee.xml"); | |
Scanner input = null; | |
try { | |
input = new Scanner(System.in); | |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | |
DocumentBuilder builder = factory.newDocumentBuilder(); | |
Document document = builder.parse(file); | |
System.out.println("Enter employee id : "); | |
int id = input.nextInt(); | |
Employee employee = getEmployee(document, id); | |
if (employee == null) { | |
System.out.println("Employee not exist with id = " + id); | |
} else { | |
System.out.println(employee); | |
} | |
} finally { | |
if (input != null) { | |
input.close(); | |
} | |
} | |
} | |
private static Employee getEmployee(Document document, int id) { | |
NodeList list = document.getElementsByTagName("Employee"); | |
int length = list.getLength(); | |
Employee emp = null; | |
String name = null, gender = null, role = null; | |
int empId = 0, age = 0; | |
for (int i = 0; i < length; i++) { | |
Node node = list.item(i); | |
if (node.getNodeType() == Node.ELEMENT_NODE) { | |
Element element = (Element) node; | |
if (String.valueOf(id).equals(element.getAttribute("id"))) { | |
emp = new Employee(); | |
empId = Integer.parseInt(element.getAttribute("id")); | |
emp.setId(empId); | |
name = element.getElementsByTagName("name").item(0).getTextContent(); | |
emp.setName(name); | |
age = Integer.parseInt(element.getElementsByTagName("age").item(0).getTextContent()); | |
emp.setAge(age); | |
gender = element.getElementsByTagName("gender").item(0).getTextContent(); | |
emp.setGender(gender); | |
role = element.getElementsByTagName("role").item(0).getTextContent(); | |
emp.setRole(role); | |
} | |
} | |
} | |
return emp; | |
} | |
} |
Output:
![]() |
Figure 3 |
Click here for more questions on DOM Parser.
References:
https://docs.oracle.com/javase/7/docs/api/javax/xml/parsers/DocumentBuilderFactory.html
https://docs.oracle.com/javase/7/docs/api/javax/xml/parsers/DocumentBuilder.html
https://docs.oracle.com/javase/7/docs/api/org/w3c/dom/Document.html
https://docs.oracle.com/javase/7/docs/api/org/w3c/dom/NodeList.html
https://docs.oracle.com/javase/7/docs/api/org/w3c/dom/Node.html
https://docs.oracle.com/javase/7/docs/api/org/w3c/dom/Element.html
Thank you friends, I hope you have clearly understood the solution of this problem. If you have any doubt, suggestion or query please feel free to comment below. You can also discuss this solution in our forum.
How to do search in XML file using DOM Parser in Java?
Reviewed by Rohit Agarwal
on
10/04/2017
Rating:

No comments:
Please provide your valuable comments. If you have any suggestion please share with me I will work on it and if you have any question or doubt please ask, don't hesitate. I am your friend, i will clarify all your doubts.