View RSS Feed

My Java Tips

JAXP SAXParser class

Rate this Entry
by , 11-28-2011 at 06:19 PM (2649 Views)
You 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:

use this method to see if the parser will perform validation or not

use this method to determine if the parser can process namespaces in an XML document

Java Code:
// 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
// Get the underlying (wrapped) SAX parser
org.xml.sax.XMLReader parser = saxParser.getXMLReader();
// Use the underlying parser
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.

Submit "JAXP SAXParser class" to Facebook Submit "JAXP SAXParser class" to Digg Submit "JAXP SAXParser class" to Submit "JAXP SAXParser class" to StumbleUpon Submit "JAXP SAXParser class" to Google

Tags: None Add / Edit Tags
Java EE