- Processing XML documents with Oracle JDeveloper 11g
- Deepak Vohra
- 607字
- 2025-03-31 06:48:17
Setting the environment
Create an application (SchemaValidation, for example) and a project (SchemaValidation, for example) in JDeveloper. To create an application and a project select File|New. In the New Gallery window, select Categories|General and Items|Generic Application. Click on OK. In the Create Generic Application window, specify an Application Name and click on Next. In the Name your Generic project window, specify a Project Name and click on Finish. An application and a project get created. Next, add some XDK 11g JAR files to the project classpath. Select the project node in Application Navigator, and select Tools|Project Properties. In the Project Properties window, select Libraries and Classpath. Click on the Add Library button to add a library. In the Add Library window, select the Oracle XML Parser v2 library and click on the OK button. The Oracle XML Parser v2 library gets added to the project Libraries. Select the Add JAR/Directory button to add JAR file xml.jar
from the C:\Oracle\Middleware\jdeveloper\modules\oracle.xdk_11.1.1
directory.

In this chapter, we shall validate an example XML document with an example XML schema using DOMParser, SAXParser
, and XSDValidator
. We shall also demonstrate error handling in validating an XML document.
First, create an XML document and an XML schema in JDeveloper. To create an XML document, select File|New. In the New Gallery window select Categories|General|XML. In the Items listed select XML Document, and click on the OK button.

In the Create XML File wizard, specify the XML file name, catalog.xml
, and click on the OK button. An XML document gets added to the SchemaValidation project in Application Navigator. To add an XML schema, select File|New, and General|XML in the New Gallery window. Select XML schema in the Items listed. Click on the OK button.

An XML schema document gets added to SchemaValidation project.

The example XML document, catalog.xml
, consists of a journal
catalog. catalog.xml
is listed in the following listing. Copy the XML document to the catalog.xml
file in the JDeveloper project.
<?xml version="1.0" encoding="utf-8"?> <catalog title="Oracle Magazine" publisher="Oracle Publishing"> <journal edition="September-October 2008"> <article section="Features"> <title>Share 2.0</title> <author>Alan Joch</author> </article> </journal> <journal edition="March-April 2008"> <article section="Oracle Developer"> <title>Declarative Data Filtering</title> <author>Steve Muench</author> </article> </journal></catalog>
As explained in the W3C XML schema Primer:"When we want to check that an instance document conforms to one or more schemas (through a process called schema validation), we need to identify which element and attribute declarations and type definitions in the schemas should be used to check which elements and attributes in the instance document."
The example XML document does not specify the location of the XML schema document to which the XML document must conform to, because we will be setting the XML schema document in the schema validation application. If the XML schema document is specified in the XML document and the schema validation application, the schema document set in the schema validation application is used.
Next, copy the example XML schema document, catalog.xsd
, listed here to catalog.xsd in the JDeveloper project Schema Validation:
<?xml version="1.0" encoding="utf-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="catalog"> <xs:complexType> <xs:sequence> <xs:element ref="journal" minOccurs="0" maxOccurs="unbounded"/> </xs:sequence> <xs:attribute name="title" type="xs:string"/> <xs:attribute name="publisher" type="xs:string"/> </xs:complexType> </xs:element> <xs:element name="journal"> <xs:complexType> <xs:sequence> <xs:element ref="article" minOccurs="0" maxOccurs="unbounded"/> </xs:sequence> <xs:attribute name="edition" type="xs:string"/> </xs:complexType> </xs:element> <xs:element name="article"> <xs:complexType> <xs:sequence> <xs:element name="title" type="xs:string"/> <xs:element name="author" type="xs:string"/> </xs:sequence> <xs:attribute name="section" type="xs:string"/> </xs:complexType> </xs:element> </xs:schema>
Each XML schema is required to be in the XML schema namespace http://www.w3.org/2001/XMLSchema. The XML schema namespace is specified with a namespace declaration in the root element, schema
, of the XML schema. A namespace declaration is of the format xmlns:xs=<namespace URL>
. Though any prefix may be used, xs
or xsd
is commonly used as shown here:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
Next, we will create Java classes for schema validation. Select File|New and subsequently Categories|General and Items|Java Class in the New Gallery window to create a Java class for schema validation. Click on the OK button. In the Create Java Class window specify a Class Name, XMLSchemaValidator
, and a package name, schemavalidation
, and click on the OK button. A Java class gets added to the SchemaValidation project. Similarly, add Java classes, DOMValidator
and SAXValidator
. The schema validation applications are shown in the Application Navigator.
