SAXParserFactory newInstance() Method



Description

The Javax.xml.parsers.SAXParserFactory.newInstance(String factoryClassName, ClassLoader classLoader) method obtains a new instance of a SAXParserFactory from class name. This function is useful when there are multiple providers in the classpath. It gives more control to the application as it can specify which provider should be loaded.

Once an application has obtained a reference to a SAXParserFactory it can use the factory to configure and obtain parser instances.

Declaration

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

public static SAXParserFactory newInstance(String factoryClassName, ClassLoader classLoader)

Parameters

  • factoryClassName − fully qualified factory class name that provides implementation of javax.xml.parsers.SAXParserFactory.

  • classLoader − ClassLoader used to load the factory class. If null current Thread's context classLoader is used to load the factory class.

Return Value

This method returns a new instance of a SAXParserFactory.

Exception

FactoryConfigurationError − if factoryClassName is null, or the factory class cannot be loaded, instantiated.

Example

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

package com.tutorialspoint;

import javax.xml.parsers.SAXParserFactory;

public class SAXParserFactoryDemo {

   public static void main(String[] args) {

      // get the SAXParserFactory implementation
      String provider =
              "com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl";

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

      try {

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

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

         // check if our factory parsers are XInclude aware
         System.out.println("" + factory.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