How to use Xerces parser in my code
843834Apr 2 2005 — edited Apr 2 2005Hello all,
This is my first time in the forumn. I am a beginer to xml. I am trying to use xerces SAX parser to parse the xml document. I have downloaded xerces 2.6.2 in the c drive. Can you please guide me how can I use it in my piece of code and how to execute. I tried using the parser available on my system. This code works fine. But I am confused with how can modify my code to parse using Xerces parser.
Help appreciated!
import javax.xml.parsers.SAXParserFactory;//This class creates instances of SAX parser factory
import javax.xml.parsers.SAXParser;//factory returns the SAXParser
import javax.xml.parsers.ParserConfigurationException;//if the parser configuration does not match throws exception
/*2 statements can be substituted by import org.xml.sax.*;
This has interfaces we use for SAX Parsers.
Has Interfaces that are used for SAX Parser.
*/
import org.xml.sax.XMLReader;
import org.xml.sax.SAXException;
import java.io.*;
import java.util.*;
public class CallSAX_18 {
public static void main (String []args)
throws SAXException,
ParserConfigurationException
IOException {
/* 3 statement below can be substitued by this below 1 statement when using other parser
XMLReader xmlReader = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
*/
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
XMLReader xmlReader = saxParser.getXMLReader();
/*/MyContentHandler has callback methods that the SAX parser will use.This class implements startElement() endElement() etc methods.
DBContentHandler dbcontent = new DBContentHandler();
xmlReader.setContentHandler(dbcontent);
xmlReader.parse(new File(args[0]).toURL().toString());
}
}