- Processing XML documents with Oracle JDeveloper 11g
- Deepak Vohra
- 731字
- 2021-08-06 17:22:09
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:

Next, create an XMLSchema
object from the schema document with which an XML document is to be validated. An XMLSchema
object represents the DOM structure of an XML schema document and is created with an XSDBuilder
class object. Create an XSDBuilder
object and invoke the build(InputSource)
method of the XSDBuilder
object to obtain an XMLSchema
object. The InputSource
object is created with an InputStream
object created from the example XML schema document, catalog.xsd
. As discussed before, we have used an InputSource
object because most SAX implementations are InputSource
based. 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 SAXParser
object with setXMLSchema(XMLSchema)
method:
saxParser.setXMLSchema(schema);
Setting the error handler
As in the previous section, define an error handling class, CustomErrorHandler
that extends DefaultHandler
class. Create an object of type CustomErrorHandler
, and register the ErrorHandler
object with the SAXParser
as shown here:
CustomErrorHandler errorHandler = new CustomErrorHandler(); saxParser.setErrorHandler(errorHandler);
Validating the XML document
The SAXParser
class extends the XMLParser
class, which provides the overloaded parse
methods discussed in the following table to parse an XML document:

Create an InputSource
object from the XML document to be validated, and parse the XML document with the parse(InputSource)
object:
InputStream inputStream=new FileInputStream(new File("catalog.xml")); InputSource inputSource=new InputSource(inputStream); saxParser.parse(inputSource);
Running the Java application
The validation application SAXValidator.java
is listed in the following listing with additional 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
SAXValidator
for SAX validation.public class SAXValidator {
- In the Java class we define a method
validateXMLDocument
.public void validateXMLDocument(InputSource input) { try {
- In the method we create a
SAXParser
and set the XML schema on theSAXParser
.SAXParser saxParser = new SAXParser(); saxParser.setValidationMode(XMLParser.SCHEMA_VALIDATION); XMLSchema schema=getXMLSchema(); saxParser.setXMLSchema(schema);
- To handle errors we create a custom error handler. We set the error handler on the
SAXParser
object and parse the XML document to be validated and also output validation errors if any.CustomErrorHandler errorHandler = new CustomErrorHandler(); saxParser.setErrorHandler(errorHandler); saxParser.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 add the Java method
getXMLSchema
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 define the
main
method in which we create an instance of theSAXValidator
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); SAXValidator validator = new SAXValidator(); validator.validateXMLDocument(inputSource); } catch (FileNotFoundException e) { System.err.println("FileNotFoundException " + e.getMessage()); } }
- Finally, we define the custom error handler class as an inner class
CustomErrorHandler
to handle validation errors.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) { } } }
Copy the SAXValidator.java
application to SAXValidator.java
in the SchemaValidation project. To demonstrate error handling, add a title
element to the journal
element.
To run the SAXValidator.java
application, right-click on SAXValidator.java in Application Navigator, and select Run. A validation error gets outputted. The validation error indicates that the title element is not expected.

- Vue.js前端開發技術
- 做好PPT就靠這幾招:圖解力+吸引力+說服力
- vtiger CRM Beginner's Guide
- Word-Excel-PowerPoint 2010三合一從新手到高手(超值版)
- Spring Security 3
- AI圖像處理:Photoshop+Firefly后期處理技術基礎與實戰
- Microsoft SharePoint 2010 Power User Cookbook: SharePoint Applied
- AutoCAD 2017從新手到高手
- Photoshop CS6標準教程(全視頻微課版)
- 邊做邊學:Photoshop CS6數碼藝術照片后期處理教程
- UG NX 12.0中文版從入門到精通
- 蝶變:移動用戶體驗設計之道
- 中文版Photoshop CS6完全自學手冊(超值版)
- AutoCAD 2024室內設計從入門到精通(升級版)
- 動畫制作基礎(項目教學版)