dcsimg

Is it possible to use a SAX parser inside a servlet? How do I set the DocumentHandler?

divya nagireddy

Yes it is possible to use a SAX parser inside a servlet.

URL url1 = new URL("http://host:port/yourfile.xml");
BufferedReader in = new BufferedReader( new InputStreamReader(url1.openStream()));
AppContentHandler myContentHandler = new AppContentHandler();
XMLReader parser = new SAXParser();
parser.setContentHandler (myContentHandler);
where AppContentHandler is a class which implements ContentHandler, hence implements all the parsing methods in the ContentHandler.