JAXP SAXParser class
16 April 2008You can doa lot if interesting stuff once you have the instance of SAXParser class. I will introduce a code snippet that will show you what you can do with an instance of SAXParser.
JAXP provides methods to determine the parser’s settings. For instance:
isValidating()
use this method to see if the parser will perform validation or not
isNamespaceAware()
use this method to determine if the parser can process namespaces in an XML document
// Get a SAX Parser instance SAXParser saxParser = saxFactory.newSAXParser(); // Find out if validation is supported boolean isValidating = saxParser.isValidating(); // Find out if namespaces are supported boolean isNamespaceAware = saxParser.isNamespaceAware(); // Parse, in a variety of ways // Use a file and a SAX DefaultHandler instance saxParser.parse(new File(args[0]), myDefaultHandlerInstance); // Use a SAX InputSource and a SAX DefaultHandler instance saxParser.parse(mySaxInputSource, myDefaultHandlerInstance); // Use an InputStream and a SAX DefaultHandler instance saxParser.parse(myInputStream, myDefaultHandlerInstance); // Use a URI and a SAX DefaultHandler instance saxParser.parse("http://www.newInstance.com/xml/doc.xml", myDefaultHandlerInstance); // Get the underlying (wrapped) SAX parser org.xml.sax.XMLReader parser = saxParser.getXMLReader(); // Use the underlying parser parser.setContentHandler(myContentHandlerInstance); parser.setErrorHandler(myErrorHandlerInstance); parser.parse(new org.xml.sax.InputSource(args[0]));
These methods can give you information about what the parser can do, but users with just a SAXParser instance —
and not the SAXParserFactory itself — do not have the means to change these features. You must do this at the parser factory level.
There are different ways to request parsing of a document. The SAXParser’s parse() method can also accept the following:
a SAX InputSource
a Java InputStream
a URL in String form
These all should have DefaultHandler instance. This means that you can still parse documents wrapped in various forms.
Related Posts:
Top Of Page | Trackback
If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.
It will look like this: JAXP SAXParser class