SAXParserFactory newInstance() Method



Description

The Javax.xml.parsers.SAXParserFactory.newInstance() method obtains a new instance of a SAXParserFactory. This static method creates a new factory instance This method uses the following ordered lookup procedure to determine the SAXParserFactory implementation class to load −

  • Use the javax.xml.parsers.SAXParserFactory system property.

  • Use the properties file "lib/jaxp.properties" in the JRE directory. This configuration file is in standard java.util.Properties format and contains the fully qualified name of the implementation class with the key being the system property defined above. The jaxp.properties file is read only once by the JAXP implementation and it's values are then cached for future use. If the file does not exist when the first attempt is made to read from it, no further attempts are made to check for its existence. It is not possible to change the value of any property in jaxp.properties after it has been read for the first time.

  • Use the Services API (as detailed in the JAR specification), if available, to determine the classname. The Services API will look for a classname in the file META-INF/services/javax.xml.parsers.SAXParserFactory in jars available to the runtime.

  • Platform default SAXParserFactory instance.

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()

Parameters

NA

Return Value

This method returns a new instance of a SAXParserFactory.

Exception

FactoryConfigurationError − if the implementation is not available or cannot be 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) {

      // 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);

         // 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