Hi,
I am relatively new to
Java. I am first time using this SAX parser to parse an XML file in my java application.Below is my handler code -
---------------------------------------------------------------------
<code>
package Sample;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import java.util.*;
public class SAXHandler extends DefaultHandler {
String a1 ;
String a2 ;
String a3;
String a4;
public void characters(char[] ch, int start, int length)
throws SAXException {
try {
a2 = new String(ch,start,length);
}
catch(Exception e) {
System.err.println(e);
}
}
public void startElement(String namespaceURI, String localName,
String qualifiedName, Attributes atts) {
if(qualifiedName.equalsIgnoreCase("Employee"))
{
a1 = atts.getValue("type");
//a2= atts.getValue("name");
System.out.println("Type = " + a1);
//System.out.println("string 2 = " + a2);
}
if (qualifiedName.equalsIgnoreCase("Name")){
System.out.println("name = " + a2);
}
if (qualifiedName.equalsIgnoreCase("Id")){
System.out.println("Id = " + a2);
}
if (qualifiedName.equalsIgnoreCase("Age")){
System.out.println("age = " + a2);
}
}
/*public void endElement(String namespaceURI, String localName,
String qualifiedName) {
try{
if (qualifiedName.equalsIgnoreCase("Name")){
System.out.println("name = " + a2);
}
if (qualifiedName.equalsIgnoreCase("Id")){
System.out.println("Id = " + a2);
}
if (qualifiedName.equalsIgnoreCase("Age")){
System.out.println("age = " + a2);
}
}
catch(Exception e) {
System.err.println(e);
}
}*/
/**
* @param args
*/
/*public static void main(String[] args) {
System.out.println("in main function");
// TODO Auto-generated method stub
}*/
}
</code>
------------------------------------------------------------------------
When I try to get the values of Id,Age and Name in the startElement() method,it prints null values.But when I create a endElement() and get the values in that method,it correctly prints the values.Please can someone explain what is the problem with my code and what is the correct way to use these 3 methods?