Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML


  Tech. Home
  Vehicle Tracking
  Tech. What is?
Jobs At RoseIndia.net!
Join For Newsletter

Powered by groups.yahoo.com
Visit Group! Post Questions!

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" ?>

<Emp_Id> E-001 </Emp_Id>
<Emp_Name> Vinod </Emp_Name>
<Emp_E-mail> Vinod1@yahoo.com </Emp_E-mail>

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

<Emp_Id> E-003 </Emp_Id>
<Emp_Name> Deepak </Emp_Name>
<Emp_E-mail> Deepak3@yahoo.com </Emp_E-mail>


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) {
      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();
        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.");
          System.out.println("Element occurrs " + list.getLength() " times in the " + str);
        System.out.println("File not found!");
    catch (Exception e){

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


Ask programming questions?



Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (post your own) View All Comments Latest 10 Comments:

it's not working . tat package cannot be found . . .

Posted by karthik on Friday, 12.5.08 @ 00:37am | #82422

  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name


Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright 2007. All rights reserved.

[an error occurred while processing this directive]