Schema validation with a SAX parser
In this section we shall validate the example XML document catalog.xml
with XML schema document catalog.xsd
, with the SAXParser
class. Import the oracle.xml.parser.schema
and oracle.xml.parser.v2
packages.
Creating a SAX parser
Create a SAXParser
object and set the validation mode of the SAXParser
object to SCHEMA_VALIDATION
, as shown in the following listing:
SAXParser saxParser=new SAXParser(); saxParser.setValidationMode(XMLParser.SCHEMA_VALIDATION);
The different validation modes that may be set on a SAXParser
are discussed in the following table; but we only need the SCHEMA-based validation modes:
Validation Mode |
Description |
---|---|
NONVALIDATING |
The parser does not validate the XML document. |
PARTIAL_VALIDATION |
The parser validates the complete or a partial XML document with a DTD or an XML schema if specified. |
DTD_VALIDATION |
The parser validates the XML document with a DTD if any. |
SCHEMA_VALIDATION |
The parser validates the XML document with an XML... |