xml sax
i am new to xml sax.i used sample sax eventreporter program .but character method behave strangely, so i can not print the desired elements as its length value gives 5 at every time and the starting also not happen desirebly.so it is really appreciated if u can advice me to resolve this matter.import org.xml.sax.*;
import org.xml.sax.helpers.*;
import java.io.*;
publicclass EventReporterimplements ContentHandler{
publicvoid setDocumentLocator(Locator locator){
//System.out.println("setDocumentLocator(" + locator + ")");
}
publicvoid startDocument()throws SAXException{
System.out.println("startDocument()");
}
publicvoid endDocument()throws SAXException{
System.out.println("endDocument()");
}
publicvoid startElement(String namespaceURI, String localName, String qName, Attributes atts)
throws SAXException{
namespaceURI ='"' + namespaceURI +'"';
localName ='"' + localName +'"';
qName ='"' + qName +'"';
String attributeString ="{";
for (int i = 0; i < atts.getLength(); i++){
attributeString += atts.getQName(i) +"=\"" + atts.getValue(i) +"\"";
if (i != atts.getLength()-1) attributeString +=", ";
}
attributeString +="}";
System.out.println("startElement(" + namespaceURI +", " + localName +", "
+ qName +", " + attributeString +")");
}
publicvoid endElement(String namespaceURI, String localName, String qName)
throws SAXException{
namespaceURI ='"' + namespaceURI +'"';
localName ='"' + localName +'"';
qName ='"' + qName +'"';
System.out.println("endElement(" + namespaceURI +", " + localName +", "
+ qName +")");
}
publicvoid characters(char[] text,int start,int length)
throws SAXException{
String textString ="[\n" + String.copyValueOf(text, start-45, start+length) +"]";
System.out.println("characters(" + textString +", " + start +", " + length +")");
}
publicvoid ignorableWhitespace(char[] text,int start,int length)
throws SAXException{
System.out.println("ignorableWhitespace()");
}
publicvoid processingInstruction(String target, String data)
throws SAXException{
System.out.println("processingInstruction(" + target +", " + data +")");
}
publicvoid startPrefixMapping(String prefix, String uri)
throws SAXException{
System.out.println("startPrefixMapping(\"" + prefix +"\", \"" + uri +"\")");
}
publicvoid endPrefixMapping(String prefix)throws SAXException{
System.out.println("endPrefixMapping(\"" + prefix +"\")");
}
publicvoid skippedEntity(String name)throws SAXException{
System.out.println("skippedEntity(" + name +")");
}
// Could easily have put main() method in a separate class
publicstaticvoid main(String[] args){
XMLReader parser;
try{
parser = XMLReaderFactory.createXMLReader();
}
catch (Exception e){
// fall back on Xerces parser by name
try{
parser = XMLReaderFactory.createXMLReader(
"org.apache.xerces.parsers.SAXParser");
}
catch (Exception ee){
System.err.println("Couldn't locate a SAX parser");
return;
}
}
if (args.length == 0){
System.out.println(
"Usage: java EventReporter URL1 URL2...");
}
// Install the content handler
parser.setContentHandler(new EventReporter());
// start parsing...
for (int i = 0; i < args.length; i++){
// command line should offer URIs or file names
try{
parser.parse(args[i]);
}
catch (SAXParseException e){// well-formedness error
System.out.println(args[i] +" is not well formed.");
System.out.println(e.getMessage()
+" at line " + e.getLineNumber()
+", column " + e.getColumnNumber());
}
catch (SAXException e){// some other kind of error
System.out.println(e.getMessage());
}
catch (IOException e){
System.out.println("Could not report on " + args[i]
+" because of the IOException " + e);
}
}
}
}

