JAXP SAXParser class

16 April 2008

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:

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.

del.icio.us:JAXP SAXParser class  digg:JAXP SAXParser class  spurl:JAXP SAXParser class  wists:JAXP SAXParser class  simpy:JAXP SAXParser class  newsvine:JAXP SAXParser class  blinklist:JAXP SAXParser class  furl:JAXP SAXParser class  reddit:JAXP SAXParser class  fark:JAXP SAXParser class  blogmarks:JAXP SAXParser class  Y!:JAXP SAXParser class  smarking:JAXP SAXParser class  magnolia:JAXP SAXParser class  segnalo:JAXP SAXParser class  gifttagging:JAXP SAXParser class

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

Leave a Reply