DocumentBuilderFactory setAttribute() Method



Description

The Javax.xml.parsers.DocumentBuilderFactory.setAttribute(String name, Object value) method allows the user to set specific attributes on the underlying implementation.

Declaration

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

public abstract void setAttribute(String name, Object value)

Parameters

  • name − The name of the attribute.

  • value − The value of the attribute.

Return Value

This method does not return a value.

Exception

IllegalArgumentException − thrown if the underlying implementation doesn't recognize the attribute.

Example

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

package com.tutorialspoint;

import javax.xml.parsers.DocumentBuilderFactory;

public class DocumentBuilderFactoryDemo {

   // get a schema language and a schema for our BuilderFactory
   static final String JAXP_SCHEMA_LANGUAGE =
      "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
   static final String W3C_XML_SCHEMA =
      "http://www.w3.org/2001/XMLSchema";

   public static void main(String[] args) {

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

      // set the attribute schema and schema language
      factory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);

      // get the attribute JAXP_SCHEMA_LANGUAGE
      System.out.println("" + factory.getAttribute(JAXP_SCHEMA_LANGUAGE));

   }
}

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

http://www.w3.org/2001/XMLSchema
javax_xml_parsers_documentbuilderfactory.htm
Advertisements