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!

XML Count Elements

                         

Here providing you an example that counts the number of tags <Emp_Id> that are used in your XML document using the SAX parser. 

Description of program:

Developing a java program, you need a XML well-formed document. When you run this program takes a xml file and checks it. If the given file exists, again it takes a tag name. Here we apply a parameterized constructor (SAXElementCount) that contains xml file. We define a default handler that implements for all SAX events and overrides two methods like: startElement() and endDocument(). The startElement method is called by parser each time. When this method overrides the code checks the given tag and counter is increased (startTag++). At last the parser reaches the end of document, the endDocument method is called and prints total number of elements. Any error occurred, exception has been thrown. If file doesn't exist it will display a message "File not found!". 

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: SAXElementCount.java

import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import java.io.*;

public class SAXElementCount{    
  int startTag = 0;
  public static String ele;
  public static void main(String args[])throws IOException {
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Enter XML file name:");
    String xmlFile = bf.readLine();
    File file = new File(xmlFile);
    if (file.exists()){
      System.out.print("Enter XML tag name:");
      ele = bf.readLine();
      SAXElementCount tagCount = new SAXElementCount(xmlFile);
    }
    else{
      System.out.println("File not found!");
    }
  }
  public SAXElementCount(String str){
    try{
      SAXParserFactory parserFact = SAXParserFactory.newInstance();
      SAXParser parser = parserFact.newSAXParser();
      DefaultHandler dHandler = new DefaultHandler(){
        public void startElement(String uri, String name, String element, 
                                      Attributes atri
)throws SAXException{
          if (element.equals(ele)){
            startTag++;
          }
        }
        public void endDocument(){
          System.out.println("Total elements: " + startTag);
        }
      };
      parser.parse(str,dHandler);
    }
    catch (Exception e){
      e.printStackTrace();
    }
  }
}

Download this example.

Output of program:

C:\vinod\xml>javac SAXElementCount.java

C:\vinod\xml>java SAXElementCount
Enter XML file name:Employee-Detail.xml
Enter XML tag name:Emp_Id
Total elements: 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]