- Processing XML documents with Oracle JDeveloper 11g
- Deepak Vohra
- 767字
- 2025-03-31 06:48:17
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 an XMLSchema
object is shown in the following listing:
XSDBuilder builder = new XSDBuilder(); InputStream inputStream=new FileInputStream(new File("catalog.xsd")); InputSource inputSource=new InputSource(inputStream); XMLSchema schema = builder.build(inputSource);
Set the XMLSchema
object on the DOMParser
object with the setXMLSchema(XMLSchema)
method:
domParser.setXMLSchema(schema);
Setting the error handler
As in the SAXParser
section, define an error handling class, CustomErrorHandler
. Create a CustomErrorHandler
object, and register the ErrorHandler
object with the DOMParser
using the setErrorHandler
method.
CustomErrorHandler errorHandler = new CustomErrorHandler(); domParser.setErrorHandler(errorHandler);
We have used a SAX-based error handler for a DOM parser because most DOM parsers use a SAX parser internally.
Parsing the XML document
The DOMParser
class extends the XMLParser
class that provides the overloaded parse
methods discussed in the previous section to parse an XML document. The preferred parse
method is parse(InputSource)
because the DOM parser uses an SAX parser internally and SAX parsers use an InputSource
object for parsing. If another input type is specified, the input is converted to an InputSource
by the SAX parser. Create an InputStream
object from the example XML document, catalog.xml
, and create an InputSource object
from the InputStream
object. Subsequently, parse the XML document using the parse(InputSource)
method:
InputStream inputStream=new FileInputStream(new File("catalog.xml")); InputSource inputSource=new InputSource(inputStream); domParser.parse(inputSource);
Running the Java application
The validation application DOMValidator.java
is listed in the following listing with explanations:
- First, we declare the
import
statements for the classes that we need.import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import oracle.xml.parser.schema.*; import oracle.xml.parser.v2.*; import java.io.IOException; import java.io.InputStream; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.InputSource;
- We define the Java class
DOMValidator
.public class DOMValidator {
- In the Java class we add a method
validateXMLDocument
.public void validateXMLDocument(InputSource input) { try {
- We create a
DOMParser
object and set the XML schema on theDOMParser
.DOMParser domParser = new DOMParser(); domParser.setValidationMode(XMLParser.SCHEMA_VALIDATION); XMLSchema schema=getXMLSchema(); domParser.setXMLSchema(schema);
- We create a
CustomErrorHandler
object and set the error handler on theDOMParser
. We parse the XML document and output the validation errors, if any.CustomErrorHandler errorHandler = new CustomErrorHandler(); domParser.setErrorHandler(errorHandler); domParser.parse(input); if (errorHandler.hasValidationError == true) { System.err.println("XML Document has Validation Error:" + errorHandler.saxParseException.getMessage()); } else { System.out.println("XML Document validates with XML schema"); } } catch (IOException e) { System.err.println("IOException " + e.getMessage()); } catch (SAXException e) { System.err.println("SAXException " + e.getMessage()); } }
- We define the
getXMLSchema
method to create anXMLSchema
object.public XMLSchema getXMLSchema() { try { XSDBuilder builder = new XSDBuilder(); InputStream inputStream = new FileInputStream(new File("catalog.xsd")); InputSource inputSource = new InputSource(inputStream); XMLSchema schema = builder.build(inputSource); return schema; } catch (XSDException e) { System.err.println("XSDException " + e.getMessage()); } catch (FileNotFoundException e) { System.err.println("FileNotFoundException " + e.getMessage()); } return null; }
- We add the
main
method in which we create an instance of theDOMValidator
class and invoke thevalidateXMLDocument
method.public static void main(String[] argv) { try { InputStream inputStream = new FileInputStream(new File("catalog.xml")); InputSource inputSource=new InputSource(inputStream); DOMValidator validator = new DOMValidator(); validator.validateXMLDocument(inputSource); } catch (FileNotFoundException e) { System.err.println("FileNotFoundException " + e.getMessage()); } }
- Finally, we define the
CustomErrorHandler
inner class.private class CustomErrorHandler extends DefaultHandler { protected boolean hasValidationError = false; protected SAXParseException saxParseException = null; public void error(SAXParseException exception) { hasValidationError = true; saxParseException = exception; } public void fatalError(SAXParseException exception) { hasValidationError = true; saxParseException = exception; } public void warning(SAXParseException exception) { } } }
To demonstrate error handling, add a title
element to the journal
element. A title
element is not valid in a journal
element.
Copy the DOMValidator.java
application to the DOMValidator.java
application in SchemaValidation project. To run the DOMValidator.java
application, right-click on DOMValidator.java in Application Navigator, and select Run. A validation error gets outputted, indicating that the title
element is not valid. If the title
element in the journal
element is removed and the DOMValidator.java
application is re-run, a validation message gets outputted to indicate that the XML document is valid.
