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);

}

}

}

}

[9266 byte] By [eIresha] at [2007-11-27 5:53:36]
# 1

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

System.out.println("[\n"+new String(ch, start, length)+"]");

}

java_2006a at 2007-7-12 15:46:56 > top of Java-index,Java Essentials,New To Java...
# 2
thank dear,i got the results.
eIresha at 2007-7-12 15:46:57 > top of Java-index,Java Essentials,New To Java...
# 3

hi all i got another matter in the content handler,at the startelement method when i try to print attributes "System.out.println(attributes.toString)" it gave the

com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser$AttributesProxy@7fdcde

rather giving the attribute of the xml document.

could you GIVE ME A SOLUTION.

eIresha at 2007-7-12 15:46:57 > top of Java-index,Java Essentials,New To Java...
# 4
That's because the toString method doesn't return the contents:Use a for loop to iterate over the attributes by using the getLength and getValue(index) methods of class Attributes.
Peetzorea at 2007-7-12 15:46:57 > top of Java-index,Java Essentials,New To Java...