How to do search in XML file using DOM Parser in Java?



Problem Description:


Suppose we have following XML file as an example.

<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>
</Employees>

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);


XML Search using Java DOM Parser
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.
          Node node = list.item(i);
  • Check whether this node is Element node or not?
          if (node.getNodeType() == Node.ELEMENT_NODE) { }
  • If it is an Element node then typecast it and get its data (attributes and properties).
          Element element = (Element) node;
  • Get its id as follows.
          String id=element.getAttribute("id");
  • 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.
           String value = element.getElementsByTagName(property_name).item(0).getTextContent();


Software Required:

  1. Java
  2. Eclipse
  3. 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 XML DOM Parser Structure part 3
Figure 2

Java Code:


employee.xml
<?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


Employee.java


SearchXML.java


About Author:

I am simple guy with lot of ambitions. My main motive is to share whatever knowledge I have related to programming. With me you can easily learn how to solve any programming problem in Java.You can connect with me on social networking sites also.


Let's Get Connected: Linkedin | Facebook |

How to do search in XML file using DOM Parser in Java? How to do search in XML file using DOM Parser in Java? Reviewed by Rohit Agarwal on 10/04/2017 Rating: 5

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.

Powered by Blogger.