Home Xml Dom Searching an Element in the given XML Document



Searching an Element in the given XML Document
Posted on: June 2, 2007 at 12:00 AM
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

     

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 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> Vinod1@yahoo.com </Emp_E-mail>
</Employee>

<Employee>
<Emp_Id> E-002 </Emp_Id>
<Emp_Name> Amit </Emp_Name>
<Emp_E-mail> Amit2@yahoo.com </Emp_E-mail>
</Employee>

<Employee>
<Emp_Id> E-003 </Emp_Id>
<Emp_Name> Deepak </Emp_Name>
<Emp_E-mail> Deepak3@yahoo.com </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

Related Tags for Searching an Element in the given XML Document:
cxmlapachefiledomparsehelpparsermethodparsersnameusingthischeckdefineifforwithexistsprogramramxerceseilitdocceinmlasmntparesstsrcmeprododefinedsxepackisivandarxmxissthstapexifinorgprndomogro


More Tutorials from this section

Ask Questions?    Discuss: Searching an Element in the given XML Document   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.