Chapter 1. Creating and Parsing an XML Document
One of the first programming exercises an XML developer wants to do is create and parse an XML document. The Java API for XML Processing (JAXP) includes an API to create and parse XML documents. Oracle XDK 11g provides an API in the oracle.xml.parsers.v2
package that overrides some of the classes in the JAXP API. It also implements some additional interfaces such as DocumentEditVAL, ElementEditVAL
, and NSResolver
, which we shall discuss in later chapters. XDK also provides parser factory and parser classes in the oracle.xml.jaxp
package that override the parser classes in the javax.xml.parsers
package.
In this chapter we shall create an XML document, catalog.xml
, and parse the XML document in JDeveloper.
The XML document that will be created and parsed is listed here:
<?xml version = '1.0' encoding = 'UTF-8'?> <catalog> <journal:journal journal:title="Oracle Magazine" journal:publisher="Oracle Publishing" journal:edition= "March-April 2008" xmlns:journal= "http://xdk.com/catalog/journal"> <journal:article journal:section="Oracle Developer"> <journal:title>Declarative Data Filtering</journal:title> <journal:author>Steve Muench</journal:author> </journal:article> </journal:journal> <journal title="Oracle Magazine" publisher="Oracle Publishing" edition="September-October 2008 "> <article section="FEATURES"> <title>Share 2.0</title> <author>Alan Joch</author> </article> </journal> </catalog>
Some of the elements and attributes in the example XML document are in the namespace identified by URI http://xdk.com/catalog/journal, and namespace prefix journal
. For example, the journal:journal
element and the journal:edition
attribute namespace nodes are included to demonstrate the creating and parsing of namespace nodes respectively.
The APIs of the oracle.xml.parsers.v2
and oracle.xml.jaxp
packages are used to create and parse example XML documents. The oracle.xml.parsers.v2
package provides APIs for DOM (Document Object Model) and SAX (Simple API for XML) parsing. The oracle.xml.jaxp
package provides APIs for obtaining parsers for DOM and SAX parsing.
Setting the environment
Download and install Oracle JDeveloper 11g Production. Create an application workspace in JDeveloper with File|New. In the New Gallery window select Categories|General and Items|General Application, and click on OK. The Create Generic Application wizard gets started. Specify an application name in the Application Name field and click on the Next button. In the Name your Generic project window, specify a project name in the Project Name field. From the Project Technologies list select JSP and Servlets and click on the Finish button. An application and a project get added to JDeveloper Application Navigator.
Next, we need to create applications for creating an XML document, parsing an XML document with the DOM API, and parsing an XML document with the SAX API. Select the Projects node in the Application Navigator and select File|New. In the New Gallery window select Categories|General and Items|Java Class, and click on the OK button. In the Create Java Class window specify a class name, CreateXMLDocument
for example, in the Name field, a package name in the Package field, and click on the OK button. Similarly, add Java applications DOMParserApp
and SAXParserApp
to the JDeveloper project.
Next, we need to add XDK parser classes to the classpath of the XMLParser
project. 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 Oracle XML Parser v2 library and click on the OK button.
The Oracle XML Parser v2 library gets added to the project libraries. Click on the OK button in the Project Properties window.