File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why we need to specify DefaultHandler methods in DefaultHandler block in sax Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why we need to specify DefaultHandler methods in DefaultHandler block in sax" Watch "why we need to specify DefaultHandler methods in DefaultHandler block in sax" New topic
Author

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

naveena kamsali
Greenhorn

Joined: Sep 20, 2011
Posts: 8
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

Joined: Nov 01, 2011
Posts: 451
handler is an object that extends DefaultHandler class and overrides DefaultHandler's methods.
naveena kamsali
Greenhorn

Joined: Sep 20, 2011
Posts: 8
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...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why we need to specify DefaultHandler methods in DefaultHandler block in sax
 
Similar Threads
using abstract method and class
Xml Parsing
issue reading xml generated from excel
XML as calculator input
SAX