Question: XML



Share on Google+Share on Google+

1 Answer(s)      6 years and 11 months ago
Posted in : XML
Write a program using SAX that will count the number of occurrences of each element type in an XML document and display them. The document file to be processed should be identified by the first command-line argument.
View Answers

July 2, 2010 at 5:49 PM

Hi Friend,

Try the following code:

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

public class CountElements{
int startTag = 0;
public static String ele;
public static void main(String args[])throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(;
System.out.print("Enter XML file name:");
String xmlFile = bf.readLine();
File file = new File(xmlFile);
if (file.exists()){
CountElements tagCount = new CountElements(xmlFile);
System.out.println("File not found!");
public CountElements(String str){
SAXParserFactory parserFact = SAXParserFactory.newInstance();
SAXParser parser = parserFact.newSAXParser();
DefaultHandler dHandler = new DefaultHandler(){
private Hashtable tags;
public void startDocument() throws SAXException {
tags = new Hashtable();
public void startElement(String namespaceURI, String localName,
String qName, Attributes atts)
throws SAXException
String key = qName;
Object value = tags.get(key);
if (value == null) {
tags.put(key, new Integer(1));
} else {
int count = ((Integer)value).intValue();
tags.put(key, new Integer(count));
public void endDocument() throws SAXException {
Enumeration e = tags.keys();
while (e.hasMoreElements()) {
String tag = (String)e.nextElement();
int count = ((Integer)tags.get(tag)).intValue();
System.out.println(tag + " occurs " + count + " times");
catch (Exception e){



Related Tutorials/Questions & Answers:
XML  How i remove a tag from xml and update it in my xml
xml  validate student login using xml for library management system
xml  validate student login using xml for library management system
xml  why the content written in xml is more secure
xml  what is name space,xml scema give an example for each   XML Namespaces provide a method to avoid element name conflicts.They are used for providing uniquely named elements and attributes in an XML document
XML  create flat file with 20 records. Read the records using xml parser and show required details
XML  please tell me how i remove one tag out of all similar type of tags in xml
xml  how can i remove white space and next line when i copy stream to xml file
XML  Hi...... Please tel me about that Aren't XML, SGML, and HTML all the same thing? Thanks
xml  what is xml   Extensible Markup Language (XML... that is both human-readable and machine-readable. It is defined in the XML 1.0... gratis open standards. The design goals of XML emphasize simplicity, generality
xml  Design an XML to maintain book details to do the following: (i) Separate Data (ii) Exchange Data (iii) Store Data (iv) Create new language
XML  Design an XML to maintain book details to do the following: (i) Separate Data (ii) Exchange Data (iii) Store Data (iv) Create new language
XML  What is specific definatio of XML? Tell us about marits and demarits of XML? use of XML? How we can use of XML?  Hi, XML... language much like HTML used to describe data. In XML, tags are not predefined
XML XSD validation in java  Can anyone help in writing validation for XML XSD in Java
XML  How to run XML program?  Hi Friend, Please visit the following links: Hope that it will be helpful for you. Thanks
XML  How to convert database to XML file?   Hi Friend, Do you want to store database data into xml file ? Please clarify this. Thanks
xml - XML
friend, XPath : It is a language for finding information in an XML document. It is used to navigate through elements and attributes in an XML document. It is a language for selecting nodes from an XML document. It is used
xml - XML
xml  how to match to xml file?can you give example with java code
xml - XML
xml  hi convert xml document to xml string.i am using below code... = stw.toString(); after i am getting xml string result, like Successxxx profile... Friend, Please visit the following link:
xml - XML
xml  define parser and xml,in jaxp api how to up load parsers... then the program contains syntax errors. XML is defined as an extensible... information,Examples and Tutorials on Xml visit to : http
xml - XML
xml  hi convert xml file to xml string in java ,after getting the xml string result and how to load xml string result? this my problem pls help..."); FileOutputStream output = new FileOutputStream("new.xml"); ReadXML xml = new
XML  Write a program using SAX that will count the number of occurrences of each element type in an XML document and display them. The document file...)); System.out.print("Enter XML file name:"); String xmlFile = bf.readLine
Consolidated XML to individual XML - XML
Consolidated XML to individual XML  How to separate the consolidated XML file to separate the individual XML names, when a user import the consolidated XML file
Creating XMl file - XML
Creating XMl file   I went on this page: and it shows me how to create an XML file, however there is something I don't understand. I have to create an XML file
xml certification dumps - XML
xml certification dumps  hi need information. xml certification purpose dumps
xml code - XML
xml code  I Read xml is used to store and transfer data can you explain it with sample code
snmp /xml gateway - XML
snmp /xml gateway  hi i would like to develop a snmp management application based on xml for that i need to construct a snmp/xml gateway which translates the xml queries into snmp command and inturn translates the returned
xml_append - XML
xml_append  hi i have a xml file im.xml its contents: asaddasahellojim i have to add more tags to .. pls help me to solve this.. ...:// Thanks
tags in xml - XML
tags in xml  Can I use : (colon )in xml tag? For ex. Is this valid?  Hi Friend, You can use colon(:) in XML tag.But is not valid. Thanks
Create XMl dynamically - XML
Create XMl dynamically  Hi I am retreiving the list from database which i need to display in an XML file with some nodes How can I do
xml problem - XML
xml problem  URGENT>>>>> how to retrieve data from hash tables without a JDBC connection?????? Thanks & Regards SAIBHARGAV146
Java Spring Hibernate Struts Training were to start with xml, XSD and java write a program that Implement an integrated COM client server to add two visuall studio 2010 Building a Parent Child relationship XML how i connect two page xml question struts cfg file for DispatchAction with mysql db Creating custom output using XMLUnit - MatchTracker duplicates the output data Related to XML Related to XML Reading child of child nodes in xml Reading multiple xml files Spliting Large XML and one by one Moved to Output Folder XMl and Java web services project download xml file from website using java code Download how to create an xml file in following clear format Conditional increment in xslt Conditional increment in xslt Conditional increment in xslt HOW TO STORE MULTIPLE EMPLOYEE DETAILS TO XML FILE USING JSP? xml and servlets xml and servlets Question about parsing XML document with DOM in JAVA. Removal of XML header Converting PHP page with flash to JSP page without flash saving data in xml Doubt about Xstream Converter Reading xml file using dom parser in java with out using getelementby tag name xml dynamically how to change attiribute values in xml XML Schema xml xml sax parser for xml parsing xml using sax xml to html via java i want java code for this xml file...please show me.. SAX Parser for huge XML file xml parsing "opening video data error" in j2me XML to JSP mapview1 mapview searchbar searchbar jsonparsing xmlparsing retrieve multiple attribute values retrieve multiple attribute values


Advertisement null