Javax.xml.parsers.SAXParser.setProperty() Method



Description

The setProperty() method in the javax.xml.parsers.SAXParser.setProperty() class is used to set specific properties related to the SAX (Simple API for XML) parser. Where SAX is an event-driven API for parsing XML documents.

Declaration

Following is the declaration for Javax.xml.parsers.SAXParser.setProperty() method

Code

public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException;

Parameters

  • name − Its a string representing the name of the property to be set.
  • value − its an Object representing the value to assign to the specified property.

Return Value

Its a void method, It doesnt return any value.

Exceptions

  • SAXNotRecognizedException − When the SAX parser does not recognize the property name.
  • SAXNotSupportedException − When the property name is recognized but the value is not supported.

Example

The following example shows the usage of Javax.xml.parsers.SAXParser.setProperty() method.

package com.tutorialspoint;

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

public class SaxParserDemo {

   public static void main(String[] args) {

      // Define a property commonly used for XML parsing
      final String property = "http://xml.org/sax/features/namespaces";

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

      try {
         // Create a new SAXParser instance
         SAXParser parser = factory.newSAXParser();

         // Set the property (enable namespace processing)
         parser.setProperty(property, Boolean.TRUE);

         // Get the property value and print it
         System.out.println("Property value: " + parser.getProperty(property));

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

Output

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

Property value: true
javax_xml_parsers_saxparser.htm
Advertisements