DocumentBuilderFactory newInstance() Method



Description

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

  • Use the javax.xml.parsers.DocumentBuilderFactory 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.DocumentBuilderFactory in jars available to the runtime.

  • Platform default DocumentBuilderFactory instance.

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

Declaration

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

public static DocumentBuilderFactory newInstance()

Parameters

NA

Return Value

This method returns a new instance of a DocumentBuilderFactory

Exception

FactoryConfigurationError − if the implementation is not available or cannot be instantiated.

Example

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

package com.tutorialspoint;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

public class DocumentBuilderFactoryDemo {

   public static void main(String[] args) {

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

      try {
         // create a new DocumentBuilder
         DocumentBuilder builder = factory.newDocumentBuilder();

         // check the configuration for namespace aware
         System.out.println("" + builder.isNamespaceAware());

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

   }
}

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

true
javax_xml_parsers_documentbuilderfactory.htm
Advertisements