Searching an Element in the given XML Document

In this you will learn to search an element in the specified XML document using DOM APIs defined in the org.apache.xerces.parsers.DOMParser package.

Searching an Element in the given XML Document

Searching an Element in the given XML Document

     

In this you will learn to search an element in the specified XML document using DOM APIs defined in the org.apache.xerces.parsers.DOMParser  package. Your classpath must  contain  xercesImpl.jar  and xml-apis.jar files  to run this program.
You can download it from Xerces

Description of program:

This program asks for a  XML file name and checks it existence. If the given file exists then it parses  the file using the parse() method. This method parses the XML document with the help of DOMParser. The DOMParser is defined in the org.apache.xerces.parsers.DOMParser. It is very useful for to create the DOMParser object. This program asks for a  xml element name that have to be searched in the XML document. Use the getLength() method for counting the occurrences of a given element  in XML document. If it counts to '0' that means element doesn't found and it displays "Element doesn't exist in the Employee-Detail.xml Document." Else it counts a given element 

Here is the video insturction "How to search element in XML using Java?":

Here is the XML File: Employee-Detail.xml

<?xml version = "1.0" ?>
<Employee-Detail>

<Employee>
<Emp_Id> E-001 </Emp_Id>
<Emp_Name> Vinod </Emp_Name>
<Emp_E-mail> [email protected] </Emp_E-mail>
</Employee>

<Employee>
<Emp_Id> E-002 </Emp_Id>
<Emp_Name> Amit </Emp_Name>
<Emp_E-mail> [email protected] </Emp_E-mail>
</Employee>

<Employee>
<Emp_Id> E-003 </Emp_Id>
<Emp_Name> Deepak </Emp_Name>
<Emp_E-mail> [email protected] </Emp_E-mail>
</Employee>

</Employee-Detail>

Here is the Java File: SearchElement.java

import org.w3c.dom.*;
import org.apache.xerces.parsers.DOMParser;
import java.io.*;

public class SearchElement{
  public static void main(String[] args) {
  try{
  BufferedReader bf = new BufferedReader(
  
new InputStreamReader(System.in));
  System.out.print("Enter file name: ");
  String str = bf.readLine();
  File file = new File(str);
  if (file.exists()){
  DOMParser parser = new DOMParser();
  parser.parse(str);
  Document doc = parser.getDocument();
  System.out.print("Enter element that have to count: ");
  String ele = bf.readLine();
  NodeList list = doc.getElementsByTagName(ele);
  if(list.getLength() == 0){
  System.out.println("Element doesn't exist in the " 
  str + 
" Document.");
  }
  else{
  System.out.println("Element occurrs " 
   list.getLength
() " times in the " + str);
  }
  }
  else{
  System.out.println("File not found!");
  }
  }
  catch (Exception e){
  e.getMessage();
  }
  }
}

Download this example.

Output of program:

C:\vinod\xml>javac SearchElement.java

C:\vinod\xml>java SearchElement
Enter file name: Employee-Detail.xml
Enter element that have to count: Emp_name
Element doesn't exist in the Employee-Detail.xml Document.

C:\vinod\xml>java SearchElement
Enter file name: Employee-Detail.xml
Enter element that have to count: Emp_Name
Element occurrs 3 times in the Employee-Detail.xml

Download source code of the project in Eclipse Project format.