SAXParserFactory newSAXParser() Method



Description

The Javax.xml.parsers.SAXParserFactory.newSAXParser() method creates a new instance of a SAXParser using the currently configured factory parameters.

Declaration

Following is the declaration for Javax.xml.parsers.SAXParserFactory.newSAXParser() method

public abstract SAXParser newSAXParser()

Parameters

NA

Return Value

This method returns a new instance of a SAXParser.

Exception

  • ParserConfigurationException − if a parser cannot be created which satisfies the requested configuration.

  • SAXException − for SAX errors.

Example

The following example shows the usage of Javax.xml.parsers.SAXParserFactory.newSAXParser() method.

package com.tutorialspoint;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

public class SAXParserFactoryDemo {

   public static void main(String[] args) {

      // create a new SAXParserFactory
      SAXParserFactory factory = SAXParserFactory.newInstance();

      try {

         // check if our factory parsers are XInclude aware
         System.out.println("" + factory.isXIncludeAware());

         // change this setting
         factory.setXIncludeAware(true);

         // create a parser
         SAXParser parser = factory.newSAXParser();

         // check the configuration of XInclude awareness
         System.out.println("" + parser.isXIncludeAware());

      } catch (Exception ex) {
         ex.printStackTrace();
      }

   }
}

If we compile the code and execute it, this will produce the following result −

false
true
javax_xml_parsers_saxparserfactory.htm
Advertisements