What is the difference between below two snippet, if i just have to parse the XML?
1.By using SAXParser parse
method:
SAXParserFactory sfactory = SAXParserFactory.newInstance();
SAXParser parser = sfactory.newSAXParser();
parser.parse(new File(filename), new DocHandler());
Now using XMLReader's parse
method acquired from SAXParser
SAXParserFactory sfactory = SAXParserFactory.newInstance();
SAXParser parser = sfactory.newSAXParser();
XMLReader xmlparser = parser.getXMLReader();
xmlparser.setContentHandler(new DocHandler());
xmlparser.parse(new InputSource("test1.xml"));
Despite of getting more flexibility, is there any other difference?
The parse
methods of SAXParser
just delegate to an internal instanceof XMLReader
and are usually more convenient. For some more advanced usecases you have to use XMLReader
. Some examples would be
ContentHandler
, EntityResolver
or ErrorHandler