Schema validation with a DOM parser
In this section, we shall validate the example XML document, catalog.xml
, against catalog.xsd
with the DOMParser
class. The procedure to validate with a DOMParser
is the same as with a SAXParser
, except that the parser class is different. First, import the oracle.xml.parser.schema
and the oracle.xml.parser.v2
packages.
Creating a DOM parser
Create a DOMParser
object and set validation mode to SCHEMA_VALIDATION
, as shown in the following listing:
DOMParser domParser=new DOMParser(); domParser.setValidationMode(XMLParser.SCHEMA_VALIDATION);
Create an XMLSchema
object, which represents the DOM structure of an XML schema document, from the example schema document. To create an XMLSchema
object, first create an XSDBuilder
object. Next, create an InputStream
object from catalog.xsd
and subsequently create an InputSource
object from the InputStream
object. Create an XMLSchema
object with the build(InputSource)
method of the XSDBuilder
class. The procedure to obtain...