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

Features

Technology
  Tech. Home
  GPS
  OSI
  WiMAX
  VoIP
  WiFi
  HSDPA
  LBS
  Vehicle Tracking
  SCADA
  Tech. What is?
Jobs At RoseIndia.net!
 
Join For Newsletter

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

To Count The Elements in a XML File

                         

In this section, you will learn to count the element in 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 takes a file name from the console and checks its availability. If the file exists then  DOMParser is created  using the org.apache.xerces.parsers.DOMParser package.This object parses the given XML document. It asks the element name and counts its occurence in the xml file. If the given element doesn't exist it displays the '0' 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: CountNodes.java

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

public class CountNodes{
  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);
        System.out.println("Number of nodes: " + list.getLength());
      }
      else{
        System.out.println("File not found!");
      }
    }
    catch (Exception e){
      e.getMessage();
    }
  }
}

Download this example.

Output of program:

C:\vinod\xml>javac CountNodes.java

C:\vinod\xml>java CountNodes
Enter file name: Employee-Detail.xml
Enter element that have to count: Emp_Name
Number of nodes: 3

                         

Ask programming questions?

 

 

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

Current Comments

0 comments so far (post your own) View All Comments Latest 10 Comments:
  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]