• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
naveena kamsali
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • 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();
}
}



}



 
Helen Ma
Ranch Hand
Posts: 451
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
handler is an object that extends DefaultHandler class and overrides DefaultHandler's methods.
 
naveena kamsali
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • 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...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic