• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

why we need to specify DefaultHandler methods in DefaultHandler block in sax

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi friends,

I have developed a simple sax program. But i didn't understand why we are writing DefaultHandler methods(startElement(),endElement() and characters()) in DefaultHandler block. Can anybody know about this. Please tell me.


package com.sax;

import java.io.IOException;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class Simple_sax{

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {

SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();

DefaultHandler handler = new DefaultHandler()


{
boolean bfname = false;
boolean blname = false;
boolean bnname = false;
boolean bsalary = false;

public void startElement(String uri, String localName,
String qName, Attributes attributes)
throws SAXException {

System.out.println("Start Element :" + qName);

if (qName.equalsIgnoreCase("FIRSTNAME")) {
bfname = true;
}

if (qName.equalsIgnoreCase("LASTNAME")) {
blname = true;
}

if (qName.equalsIgnoreCase("NICKNAME")) {
bnname = true;
}

if (qName.equalsIgnoreCase("SALARY")) {
bsalary = true;
}

}//startElement

public void endElement(String uri, String localName,
String qName)
throws SAXException {

System.out.println("End Element :" + qName);

}

public void characters(char ch[], int start, int length)
throws SAXException {

if (bfname) {
System.out.println("First Name : "
+ new String(ch, start, length));
bfname = false;
}

if (blname) {
System.out.println("Last Name : "
+ new String(ch, start, length));
blname = false;
}

if (bnname) {
System.out.println("Nick Name : "
+ new String(ch, start, length));
bnname = false;
}

if (bsalary) {
System.out.println("Salary : "
+ new String(ch, start, length));
bsalary = false;
}

}//characters


};
saxParser.parse("sax.xml",handler);


} catch (Exception e) {
e.printStackTrace();
}
}



}



 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
handler is an object that extends DefaultHandler class and overrides DefaultHandler's methods.
 
naveena kamsali
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

naveena kamsali wrote:hi friends,

I have developed a simple sax program. But i didn't understand why we are writing DefaultHandler methods(startElement(),endElement() and characters()) in DefaultHandler block. Can anybody know about this. Please tell me.


package com.sax;

import java.io.IOException;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class Simple_sax{

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {

SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();

DefaultHandler handler = new DefaultHandler()


{
boolean bfname = false;
boolean blname = false;
boolean bnname = false;
boolean bsalary = false;

public void startElement(String uri, String localName,
String qName, Attributes attributes)
throws SAXException {

System.out.println("Start Element :" + qName);

if (qName.equalsIgnoreCase("FIRSTNAME")) {
bfname = true;
}

if (qName.equalsIgnoreCase("LASTNAME")) {
blname = true;
}

if (qName.equalsIgnoreCase("NICKNAME")) {
bnname = true;
}

if (qName.equalsIgnoreCase("SALARY")) {
bsalary = true;
}

}//startElement

public void endElement(String uri, String localName,
String qName)
throws SAXException {

System.out.println("End Element :" + qName);

}

public void characters(char ch[], int start, int length)
throws SAXException {

if (bfname) {
System.out.println("First Name : "
+ new String(ch, start, length));
bfname = false;
}

if (blname) {
System.out.println("Last Name : "
+ new String(ch, start, length));
blname = false;
}

if (bnname) {
System.out.println("Nick Name : "
+ new String(ch, start, length));
bnname = false;
}

if (bsalary) {
System.out.println("Salary : "
+ new String(ch, start, length));
bsalary = false;
}

}//characters


};
saxParser.parse("sax.xml",handler);


} catch (Exception e) {
e.printStackTrace();
}
}



}










thanks friends i got the answer...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic