- Javax.xml.bind classes
- Home
- Binder
- DatatypeConverter
- JAXB
- JAXBContext
- JAXBElement
- JAXBElement.GlobalScope
- JAXBIntrospector
- Marshaller.Listener
- SchemaOutputResolver
- Unmarshaller.Listener
- Javax.xml.bind.util classes
- JAXBResult
- JAXBSource
- ValidationEventCollector
- Javax.xml.parsers classes
- DocumentBuilder
- DocumentBuilderFactory
- SAXParser
- SAXParserFactory
- Javax.xml.soap classes
- AttachmentPart
- MessageFactory
- MimeHeader
- MimeHeaders
- SAAJMetaFactory
- SOAPConnection
- SOAPConnectionFactory
- SOAPFactory
- SOAPMessage
- SOAPPart
- Javax.xml.validation classes
- Schema
- SchemaFactory
- TypeInfoProvider
- Validator
- ValidatorHandler
- Javax.xml.xpath classes
- XPathConstants
- XPathFactory
- Java Useful Resources
- Java - Quick Guide
- Java - Useful Resources
DocumentBuilderFactory setSchema() Method
Description
The Javax.xml.parsers.DocumentBuilderFactory.setSchema(Schema schema) method Set the Schema to be used by parsers created from this factory.
When a Schema is non-null, a parser will use a validator created from it to validate documents before it passes information down to the application.
When errors are found by the validator, the parser is responsible to report them to the user-specified ErrorHandler (or if the error handler is not set, ignore them or throw them), just like any other errors found by the parser itself. In other words, if the user-specified ErrorHandler is set, it must receive those errors, and if not, they must be treated according to the implementation specific default error handling rules.
A validator may modify the outcome of a parse (for example by adding default values that were missing in documents), and a parser is responsible to make sure that the application will receive modified DOM trees.
Declaration
Following is the declaration for Javax.xml.parsers.DocumentBuilderFactory.setSchema() method
public void setSchema(Schema schema)
Parameters
schema − Schema to use or null to remove a schema.
Return Value
This method does not return a value.
Exception
UnsupportedOperationException − When implementation does not override this method.
Example
The schema set in this example is a file called Student.xsd and is in our CLASSPATH. The contents of this file are the following −
<?xml version = "1.0" encoding = "utf-8"?> <xs:schema attributeFormDefault = "unqualified" elementFormDefault = "qualified" xmlns:xs = "http://www.w3.org/2001/XMLSchema"> <xs:element name = "student"> <xs:complexType> <xs:sequence> <xs:element name = "age" type = "xs:unsignedByte" /> <xs:element name = "name" type = "xs:string" /> </xs:sequence> <xs:attribute name = "id" type = "xs:unsignedByte" use = "required" /> </xs:complexType> </xs:element> </xs:schema>
The following example shows the usage of Javax.xml.parsers.DocumentBuilderFactory.setSchema() method.
package com.tutorialspoint; import java.io.File; import javax.xml.XMLConstants; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; public class DocumentBuilderFactoryDemo { public static void main(String[] args) { // create a new DocumentBuilderFactory DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { File file = new File("Student.xsd"); // create schema String constant = XMLConstants.W3C_XML_SCHEMA_NS_URI; SchemaFactory xsdFactory = SchemaFactory.newInstance(constant); Schema schema = xsdFactory.newSchema(file); // set schema factory.setSchema(schema); // get the schema 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@2c091cee
To Continue Learning Please Login
Login with Google