SAXParserFactory getSchema() Method



Description

The Javax.xml.parsers.SAXParserFactory.getSchema() method gets the Schema object specified through the setSchema(Schema schema) method.

Declaration

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

public Schema getSchema()

Parameters

NA

Return Value

This method returns the Schema object that was last set through the setSchema(Schema) method, or null if the method was not invoked since a SAXParserFactory is created.

Exception

UnsupportedOperationException − When implementation does not override this method.

Example

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

package com.tutorialspoint;

import java.io.File;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import static javax.xml.XMLConstants.*;

public class SAXParserFactoryDemo {

   public static void main(String[] args) {

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

      // create a new schema factory
      SchemaFactory schemaFactory =
         SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI);

      // a file that points to our schema
      File file = new File("Student.xsd");

      try {
         // create a new schema
         Schema schema = schemaFactory.newSchema(file);

         // set our schema for our parser
         factory.setSchema(schema);

         // get the schema set
         System.out.println("" + factory.getSchema());

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

   }
}

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

com.sun.org.apache.xerces.internal.jaxp.validation.SimpleXMLSchema@211d0a4f
javax_xml_parsers_saxparserfactory.htm
Advertisements