Home Web Development Processing XML documents with Oracle JDeveloper 11g

Processing XML documents with Oracle JDeveloper 11g

books-svg-icon Book
eBook $36.99 $24.99
Print $60.99
Subscription $15.99 $10 p/m for three months
$10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
BUY NOW $10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
eBook $36.99 $24.99
Print $60.99
Subscription $15.99 $10 p/m for three months
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
  1. Free Chapter
    Creating and Parsing an XML Document
About this book
XML is an open standard for creating markup languages and exchanging structured documents and data over the Internet. JDeveloper 11g presents an effective, quick, and easy-to-use means of processing XML documents. Inspired by the author's previous XML articles for the Oracle community, this expanded hands-on tutorial guides newcomers and intermediate users through JDeveloper 11g and XML document development. It offers up-to-date information on working with the latest version of JDeveloper, and brand new information on JAXB 2.0 support in JDeveloper 11g. Filled with illustrations, explanatory tables, and comprehensive instructions, this book walks the reader through the wide assortment of JDeveloper's capabilities. Oracle's JDeveloper 11g is an Integrated Development Environment that provides a visual and declarative approach to application development. Over the course of 14 chapters, readers will get hands-on with JDeveloper as the comprehensive and self-contained tutorials provide clear instruction on the key XML tasks that JDeveloper can accomplish. Filled with practical information and illustrated examples, this book shows the reader how to create, parse, and store XML documents quickly, as well as providing step-by-step instructions on how to construct an XML schema and use the schema to validate an XML document. Oracle's XML Developer Kit (XDK) offers a set of components, tools, and utilities for developing XML-based applications, and developers will find the detailed XDK coverage invaluable. Later chapters are given over to using XPath, transforming XML with XSLT, and using the JSTL XML Tag Library. Moving through the book, a chapter on the JAXB 2.0 API shows you how to bind, marshal and unmarshal XML documents, before we finally delve into comparing XML documents, and converting them into PDF and Excel formats. In all, this book will enable the reader to gain a good and wide-ranging understanding of what JDeveloper has to offer for XML processing.
Publication date:
May 2009
Publisher
Packt
Pages
384
ISBN
9781847196668

 

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.

 

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.

 

Generating an XML document


In this section we shall generate an XML document in JDeveloper. The example XML document in the introduction will be created by the CreateXMLDocument.java application. First, import the DOM and SAX parsing APIs package oracle.xml.parser.v2, and the DOM and SAX parsers package oracle.xml.jaxp:

import oracle.xml.jaxp.*;
import oracle.xml.parser.v2.*;

Creating the factory

Create a JXDocumentBuilderFactory object with the static method newInstance(). The factory object is used to obtain a parser that may be used to create a new DOM object tree. The JXDocumentBuilderFactory class is the implementation class in Oracle XDK 11g for the abstract class DocumentBuilderFactory:

JXDocumentBuilderFactory factory = (JXDocumentBuilderFactory)
JXDocumentBuilderFactory.newInstance ();

The JXDocumentBuilderFactory class extends the DocumentBuilderFactory class and provides some additional methods apart from providing some static fields and constants. The constants are used in the setAttribute(java.lang.String name, java.lang.Object value) method to set factory attribute values. The attribute names are specified as a String object and attribute values are specified as an object. The getAttribute(java.lang.String name) method may be used to retrieve the value of an attribute. Some of these attributes are listed in the following table; the attributes ERROR_STREAM and SHOW_WARNINGS will be used in the DOM parsing section:

Attribute

Description

BASE_URL

Specifies the base URL to resolve external entities. The base URL is specified as a URL object. External entities are resolved using an EntityResolver, which is set on a DOMParser or a SAXParser using the setEntityResolver(EntityResolver) method.

DEBUG_MODE

Specifies the debug mode. The debug mode value is a Boolean and value may be set to Boolean.TRUE or Boolean.FALSE.

DTD_OBJECT

Specifies the DTD object to validate the XML document. The DTD object is set as an oracle.xml.parser.v2.DTD class object. As XML Schema is the preferred standard for validating an XML document, DTDs have become archaic.

ERROR_ENCODING

Specifies the text encoding for error reports in the error stream. Error encoding is specified as a string literal such as UTF-8. The ERROR_ENCODING attribute may be set only if the ERROR_STREAM attribute is set.

ERROR_STREAM

Specifies the error stream for reporting errors. The attribute value can be an OutputStream or a PrintWriter object. If ErrorHandler is set, ERROR_STREAM is not used and is ignored.

NODE_FACTORY

Specifies the NodeFactory object for custom nodes. The NodeFactory class provides methods, which may be overridden, to create nodes of the DOM tree built during parsing. These methods are different than the XMLDocument class methods in that they return an Oracle class object instead of a standard org.w3c.dom package class object. For example, the createElement method returns an oracle.xml.parser.v2.XMLElement object instead of an org.w3c.dom.Element object.

SCHEMA_LANGUAGE

Specifies the schema language to be used for validation. If its value is set to http://www.w3.org/2001/XMLSchema an XML Schema is used for validation. If Relax NG is used, set its value to http://relaxng.org/ns/structure/1.0. If DTD is used, set its value to http://www.w3.org/TR/REC-xml.

SCHEMA_OBJECT

Specifies the schema object to be used for validation. Schema object is an oracle.xml.parser.schema.XMLSchema class object.

SCHEMA_SOURCE

Specifies the XML Schema file to be used for validation. Its value may be set to one of the following:

  • java.lang.String

  • java.io.InputStream

  • org.xml.sax.InputSource

  • java.io.File

  • An array of java.lang.Object with contents as one the previously listed types

SHOW_WARNINGS

Specifies if warnings are to be shown during schema validation. Its value is a Boolean, which may be set to Boolean.TRUE or Boolean.FALSE.

USE_DTD_ONLY_FOR_VALIDATION

Specifies if the DTD object is to be used only for validation and not to be added to the DOM document. Its value is a Boolean, which can be set to Boolean.TRUE or Boolean.FALSE.

Creating the DOM document object

Create a DocumentBuilder object from the factory object with the newDocumentBuilder() method. The DocumentBuilder object is used to create a new instance of a DOM Document object or to obtain a DOM Document object from an XML document. The JXDocumentBuilder class extends the DocumentBuilder class, and is an implementation class in Oracle XDK 11g for the abstract DocumentBuilder class. Cast the DocumentBuilder object, returned by the newDocumentBuilder() method, to the JXDocumentBuilder class:

JXDocumentBuilder documentBuilder = (JXDocumentBuilder)
factory.newDocumentBuilder();

Obtain a Document object from the JXDocumentBuilder object with the newDocument() method. The XMLDocument class implements the Document interface. Cast the Document object to XMLDocument:

XMLDocument xmlDocument = (XMLDocument)
documentBuilder.newDocument();

In addition to the Document interface, the XMLDocument class implements DocumentEditVAL, ElementEditVAL, DocumentEvent, DocumentTraversal, EventTarget, and NSResolver. The DocumentEditVAL and ElementEditVAL interfaces are implemented for dynamic validation as specified in the DOM 3 Validation specification and will be discussed in Chapter 8. The DocumentEvent and EventTarget interfaces are implemented for event handling and will be discussed in Chapter 7. The NSResolver interface is used for selecting namespace nodes with XPath, and will be discussed in Chapter 4.

The XMLDocument class provides some additional methods not specified in any of the implemented interfaces. Some of these methods are discussed in the following table:

Method

Description

addID(String, XMLElement)

Adds an element associated with an ID to the document. An ID distinguishes an element from other elements and may be used to identify and retrieve an element. The String parameter specifies the ID and the XMLElement parameter specifies the element associated with the ID.

getIDHashtable()

Returns the ID hashtable associated with the DOM tree. If you know the ID of an element, the element may be retrieved by first obtaining the hashtable of all elements associated with an ID and subsequently retrieving the element for the known ID.

adoptNode(Node srcNodeArg)

Adopts a node from another document. The node is removed from the other document. If the node is to be kept in the source document, use the importNode method instead.

getEncoding()

Gets document character encoding. The document encoding is specified in the encoding parameter. For example, <?xml encoding="UTF8" version="1.0" ?>

setEncoding(String)

Sets document character encoding. The document encoding gets set as the "encoding" parameter in the XML declaration when the document is saved.

print(PrintDriver pd)

Prints the contents of the DOM tree using the specified PrintDriver. The print method is overloaded to take the output stream as an OutputStream, PrintWriter, or Writer object in addition to a PrintDriver object.

printExternalDTD(java.io.PrintWriter out)

Prints the contents of the external DTD using the specified PrintWriter. The method is overloaded to take the output stream as an OutputStream object.

setDoctype(java.lang.String rootname,java.lang.String sysid,java.lang.String pubid)

Sets the doctype for the document. When the DOM tree is saved, the <!DOCTYPE rootname PUBLIC publicid systemid> declaration gets added to the document.

getVersion()

Returns the XML version. The XML version is specified in the "version" parameter of the XML declaration, for example <?xml version="1.0" encoding="UTF8" ?>.

setVersion()

Sets XML version.

getSchema()

Returns the XMLSchema object corresponding to the XML Schema specified in the document. An XML Schema is specified in an XML document with the xsi:noNamespaceSchemaLocation attribute or the xsi:NamespaceSchemaLocation attribute in the root element of an XML document.

setSchema(XMLSchema)

Sets XMLSchema for validation. If an XML Schema is specified in the XML document and also set in the setSchema method, the XML Schema set using the setSchema method overrides the schema specified in the document.

setStandalone(String)

Sets the standalone parameter for the XML declaration. The value is "yes" or "no". A standalone document is a document that does not contain any external markup declarations. (A parameter entity is an example of an external markup declaration.) The standalone parameter is specified because markup declarations can affect the content of the document as transferred from the XML processor to the application.

getStandalone()

Gets the standalone parameter value for the XML declaration.

setLocale(java.util.Locale locale)

Sets the locale for error reporting. For example, for the UK you would create the Locale object as:

new Locale(Locale. ENGLISH, Locale.UK).

Set the XML version of the DOM document object using the setVersion method, and the encoding of the DOM document using the setEncoding method:

xmlDocument.setVersion("1.0");
xmlDocument.setEncoding("UTF-8");

Creating the root element

Create the root element catalog with the createElement(String) method. Cast the Element object returned by the createElement() method to XMLElement:

XMLElement catalogElement = (XMLElement) (xmlDocument.createElement" ("catalog"));

The XMLElement class implements the Element interface. In addition to the Element interface, XMLElement implements the ElementEditVAL and NSResolver interfaces. The ElementEditVAL interface is used for DOM 3 Validation and the NSResolver interface is used for selecting namespace nodes with XPath, which will be discussed in Chapter 4. In addition to the validation methods from the ElementEditVAL interface, the XMLElement class has the overloaded validateContent() method to validate an element. The validation methods shall be discussed in Chapter 8.

Add the root element to the XMLDocument object using the appendChild method:

xmlDocument.appendChild(catalogElement);

Constructing the DOM document

Next, we shall create the DOM document tree:

  1. 1. Create the namespace element journal:journal with the createElementNS(String, String) method.

    XMLElement journalElement = (XMLElement)
    (xmlDocument.createElementNS("http://xdk.com/catalog/journal","journal:journal"));
    
  2. 2. Add the journal element to the root element using the appendChild method.

    catalogElement.appendChild(journalElement);
    
  3. 3. Add a namespace attribute journal:title with the setAttributeNS(String, String, String) method.

    journalElement.setAttributeNS("http://xdk.com/catalog/journal",
    "journal:title", "Oracle Magazine");
    
  4. 4. Similarly, add journal:publisher and journal:author attributes. Add journal:article and journal:title elements similar to the journal:journal element. Create an XMLText node, which represents a text node, to set the text of the title element using the createTextNode(String) method.

    XMLText title = (XMLText) xmlDocument.createTextNode
    ("Declarative Data Filtering");
    
  5. 5. Add the XMLText node to journal:title element using the appendChild method.

    titleElement.appendChild(title);
    

In the same way, add the other element and text nodes in the example XML document. The XMLDocument class provides additional methods than the Document interface methods to create XML document components other than those discussed in this section. Some of these methods are discussed in the following table:

Method Name

Description

createCDATASection(java.lang.String data)

Creates a CDATA section. A CDATA section is text that is not parsed by the parser. Special characters, which may need to be included in text data at times, generate a parser error. Text containing such data should be included in a CDATA section.

createComment(java.lang.String data)

Creates a comment. Comments are represented with <!-- -->. They are specified outside the markup and may also be specified within a document type definition, as permitted by the grammar. Comments are not included in a document's character data, but an XML processor may—though is not required to—retrieve the character data in a comment.

createEntityReference(java.lang.String name)

Creates an entity reference. An entity reference is a reference to an entity, which is data that is defined as an abbreviation or data that is found at an external location. Entity references are included to represent data that has multiple occurrences in a document.

createProcessingInstruction(java.lang.String target, java.lang.String data)

Creates a Processing Instruction. Processing Instructions are not included in a document's character data and are instructions for the application.

Outputting the DOM document

Output the DOM document object with the XMLPrintDriver class. Create an OutputStream object to output the XML document, and create an XMLPrintDriver using the OutputStream:

OutputStream output = new FileOutputStream(new File(
"catalog.xml"));
XMLPrintDriver xmlPrintDriver = new XMLPrintDriver(new
PrintWriter(output));

Output the XML document with the printDocument(XMLDocument) method. Flush the output stream using the flush() method and close the output stream using the close() method:

xmlPrintDriver.printDocument(xmlDocument);
xmlPrintDriver.flush();
xmlPrintDriver.close();

XMLPrintDriver may be used to print not only an XMLDocument node, but other nodes as well. The print methods in the XMLPrintDriver class are listed in the following table:

Print Method

Description

printAttribute(XMLAttr)

Prints an attribute node.

printAttributeNodes (XMLElement)

Prints attributes in an element node.

printCDATASection(XMLCDATA)

Prints a CDATA section node.

printChildNodes(XMLNode)

Prints child nodes for a node.

printComment(XMLComment)

Prints comment node.

printDoctype(DTD)

Prints DTD.

printDocument(XMLDocument)

Prints a document. We used this method in outputting the DOM document tree.

printDocumentFragment (XMLDocumentFragment)

Prints a document fragment. A document fragment represents a fragment of the document. Use this method if you only need to output a section of the document.

printElement(XMLElement)

Prints an element node.

printEntityReference (XMLEntityReference)

Prints an entity reference node.

printProcessingInstruction (XMLPI)

Prints a processing instruction node.

printTextNode(XMLText)

Prints a text node.

Running the Java application

To run the CreateXMLDocument.java application in JDeveloper, right-click on CreateXMLDocument.java in Application Navigator and select Run.

The XML document gets generated. Select View|Refresh to add the generated XML document, catalog.xml, to the Application Navigator.

The complete CreateXMLDocument.java Java application is listed here with brief notes that explain the different sections of the application:

  1. 1. First, we declare the package statement and the import statements.

    package xmlparser;
    import oracle.xml.jaxp.*;
    import oracle.xml.parser.v2.*;
    import java.io.*;
    import org.w3c.dom.DOMException;
    import javax.xml.parsers.ParserConfigurationException;
    
  2. 2. Next, we define the Java class CreateXMLDocument.

    public class CreateXMLDocument {
    
  3. 3. Now, we define the method to create an XML document.

    public void createXMLDocument() {
    try {
    
  4. 4. Next, we create the XMLDocument object.

    JXDocumentBuilderFactory factory =
    (JXDocumentBuilderFactory)JXDocumentBuilderFactory.newInstance();
    JXDocumentBuilder documentBuilder =
    (JXDocumentBuilder)factory.newDocumentBuilder();
    XMLDocument xmlDocument = (XMLDocument)documentBuilder.newDocument();
    xmlDocument.setVersion("1.0");
    xmlDocument.setEncoding("UTF-8");
    
  5. 5. Here, we create the root element catalog and the first subelement journal.

    XMLElement catalogElement = (XMLElement) (xmlDocument.createElement("catalog"));
    xmlDocument.appendChild(catalogElement);
    XMLElement journalElement = (XMLElement) (xmlDocument.createElementNS("http://xdk.com/catalog/journal",
    "journal:journal"));
    catalogElement.appendChild(journalElement);
    
  6. 6. Next, we add namespace attributes title, publisher, and edition to the journal element.

    journalElement.setAttributeNS("http://xdk.com/catalog/journal","journal:title", "Oracle Magazine");
    journalElement.setAttributeNS("http://xdk.com/catalog/journal","journal:publisher", "Oracle Publishing");
    journalElement.setAttributeNS("http://xdk.com/catalog/journal","journal:edition", "March-April 2008");
    
  7. 7. Now, we create the element, text, and attribute nodes in catalog.xml, the XML document.

    XMLElement articleElement = (XMLElement) (xmlDocument.createElementNS("http://xdk.com/catalog/journal","journal:article"));
    journalElement.appendChild(articleElement);
    articleElement.setAttributeNS("http://xdk.com/catalog/journal","journal:section", "Oracle Developer");
    XMLElement titleElement = (XMLElement) (xmlDocument.createElementNS("http://xdk.com/catalog/journal",
    "journal:title"));
    articleElement.appendChild(titleElement);
    XMLText title = (XMLText) xmlDocument.createTextNode ("Declarative Data Filtering");
    titleElement.appendChild(title);
    XMLElement authorElement = (XMLElement) (xmlDocument.createElementNS("http://xdk.com/catalog/journal",
    "journal:author"));
    articleElement.appendChild(authorElement);
    XMLText author = (XMLText) xmlDocument.createTextNode(
    "Steve Muench");
    authorElement.appendChild(author);
    journalElement = (XMLElement) (xmlDocument.createElement ("journal"));
    catalogElement.appendChild(journalElement);
    journalElement.setAttribute("title", "Oracle Magazine");
    journalElement.setAttribute("publisher", "Oracle Publishing");
    journalElement.setAttribute("edition", " September-October 2008");
    articleElement = (XMLElement)(xmlDocument.createElement("article"));
    journalElement.appendChild(articleElement);
    articleElement.setAttribute("section", "FEATURES");
    titleElement = (XMLElement) (xmlDocument.createElement("title"));
    articleElement.appendChild(titleElement);
    title = (XMLText) xmlDocument.createTextNode("Share 2.0");
    titleElement.appendChild(title);
    authorElement = (XMLElement)(xmlDocument.createElement("author"));
    articleElement.appendChild(authorElement);
    author = (XMLText) xmlDocument.createTextNode("Alan Joch");
    authorElement.appendChild(author);
    
  8. 8. Here, we output the XML document to the file catalog.xml.

    OutputStream output = new FileOutputStream(new
    File("catalog.xml"));
    XMLPrintDriver xmlPrintDriver = new XMLPrintDriver(new PrintWriter(output));
    xmlPrintDriver.printDocument(xmlDocument);
    xmlPrintDriver.flush();
    xmlPrintDriver.close();
    } catch (DOMException e) {
    System.err.println(e.getMessage());
    } catch (IOException e) {
    System.err.println(e.getMessage());
    } catch (ParserConfigurationException e) {
    System.err.println(e.getMessage());
    }
    }
    
  9. 9. Finally, we define the main method for the Java class. In the main method, we create an instance of the CreateXMLDocument class and invoke the createXMLDocument method.

    public static void main(String[] argv) {
    CreateXMLDocument createXMLDocument = new CreateXMLDocument();
    createXMLDocument.createXMLDocument();
    }
    }
    
 

Parsing an XML document with the DOM API


In this section we shall parse an XML document (the XML document that was created in the previous section) with a DOM parser. DOM parsing creates an in-memory tree-like structure of an XML document, which may be navigated with the DOM API. We shall iterate over the XML document parsed, and output elements and attribute node values.

The DOM parsing API classes are in the oracle.xml.parser.v2 package and the DOM parser factory and parser classes are in the oracle.xml.jaxp package. First, import these packages into the DOMParserApp.java class in JDeveloper:

import oracle.xml.jaxp.*;
import oracle.xml.parser.v2.*;

Creating the factory

Create a JXDcoumentBuilderFactory object with the static method newInstance(). The factory object is used to obtain a parser that may be used to create a DOM document tree from an XML document:

JXDocumentBuilderFactory factory = (JXDocumentBuilderFactory) JXDocumentBuilderFactory.newInstance();

Set the ERROR_STREAM and SHOW_WARNINGS attributes on the factory object with the setAttribute() method. The ERROR_STREAM attribute specifies the error stream, while the SHOW_WARNINGS attribute specifies if warnings are to be shown. The value of the ERROR_STREAM attribute is an OutputStream object or a PrintWriter object. The value of the SHOW_WARNINGS attribute is a Boolean, which can be set to Boolean.TRUE or Boolean.FALSE. With the OutputStream or PrintWriter specified in the ERROR_STREAM attribute, parsing errors (if any) get outputted to the specified file. If ErrorHandler is also set, ERROR_STREAM is not used. The SHOW_WARNINGS attribute outputs warnings also:

factory.setAttribute(JXDocumentBuilderFactory.ERROR_STREAM,
new FileOutputStream(new File("c:/output/errorStream.txt")));
factory.setAttribute(JXDocumentBuilderFactory.SHOW_WARNINGS,
Boolean.TRUE);

Creating a DOM document object

Create a JXDocumentBuilder object from the factory object by first creating a DocumentBuilder object with newDocumentBuilder() method and subsequently casting the DocumentBuilder object to JXDocumentBuilder. JXDocumentBuilder is the implementation class in Oracle XDK 11g for the abstract class DocumentBuilder:

JXDocumentBuilder documentBuilder = (JXDocumentBuilder) factory.newDocumentBuilder();

The JXDocumentBuilder object is used to create a DOM document object from an XML document. A Document object may be obtained using the JXDocumentBuilder object with one of the parse() methods in the JXDocumentBuilder class. The input to the parser may be specified as InputSource, InputStream, File object, or a String URI. Create an InputStream for the example XML document and parse the document with the parse(InputStream) method:

InputStream input = new FileInputStream(new File("catalog.xml"));
XMLDocument xmlDocument = (XMLDocument) (documentBuilder.parse(input));

The parse() methods of the JXDocumentBuilder object return a Document object, which may be cast to an XMLDocument object, as the XMLDocument class implements the Document interface.

Outputting the XML document components' values

Output the encoding in the XML document using the getEncoding method, and output the version of the XML document using the getVersion method:

System.out.println("Encoding: " + xmlDocument.getEncoding());
System.out.println("Version: " + xmlDocument.getVersion());

The XMLDocument class has various getter methods to retrieve elements in a document. Some of these methods are listed in the following table:

Method Name

Description

getDocumentElement()

Returns the root element.

getElementById(String)

Returns element for a specified ID. An element that has an ID attribute may be retrieved using this method. An attribute named "id" is not necessarily an ID attribute. An ID attribute is defined in an XML Schema with the xs:ID type and in a DTD with ID attribute type.

getElementsByTagName (String)

Returns a NodeList of elements for a specified tag name. The elements are returned in the order defined in the DOM tree. All the elements of the specified tag name are returned, not just the top-level elements. If the tag name is specified as "*", all the elements in the document are returned.

getElementsByTagNameNS(String namespaceURI, String localName)

Returns a NodeList of elements for a specified namespace URI and local name.

As an example, retrieve title elements in the namespace http://xdk.com/catalog/journal using the getElementsByTagNameNS method:

NodeList namespaceNodeList = xmlDocument.getElementsByTagNameNS("http://xdk.com/catalog/journal","title");

Iterate over the NodeList to output element namespace, element namespace prefix, element tag name, and element text. The getNamespaceURI() method returns the namespace URI of an element. The getPrefix() method returns the prefix of an element in a namespace. The getTagName() method returns the element tag name. Element text is obtained by first obtaining the text node within the element node using the getFirstChild() method and subsequently the value of the text node:

for (int i = 0; i < namespaceNodeList.getLength(); i++) {
XMLElement namespaceElement = (XMLElement) namespaceNodeList.item(i);
System.out.println("Namespace URI: " +
namespaceElement.getNamespaceURI());
System.out.println("Namespace Prefix: " +
namespaceElement.getPrefix());
System.out.println("Element Name: " +
namespaceElement.getTagName());
System.out.println("Element text: " +
namespaceElement.getFirstChild().getNodeValue());
}

Obtain the root element in the XML document with the getDocumentElement() method. The getDocumentElement method returns an Element object that may be cast to an XMLElement object if any of the methods defined only in the XMLElement class are to be used. The Element object is not required to be cast to an XMLElement object. We have cast the Element object to XMLElement as XMLElement is Oracle XDK 11g's implementation class for the Element interface, and we are discussing Oracle XDK 11g:

XMLElement rootElement = (XMLElement)
(xmlDocument.getDocumentElement());
System.out.println("Root Element is: " + rootElement.getTagName());

Next, we shall iterate over all the subnodes of the root element. Obtain a NodeList of subnodes of the root element with the getChildNodes() method. Create a method iterateNodeList() to iterate over the subnodes of an Element. Iterate over the NodeList and recursively obtain the subelements of the elements in the NodeList. The method hasChildNodes() tests to see if a node has subnodes. Ignorable whitespace is also considered a node, but we are mainly interested in the subelements in a node. The NodeList interface method getLength() returns the length of a node list, and method item(int) returns the Node at a specified index. As class XMLNode is Oracle XDK 11g's implementation class for the Node interface, cast the Node object to XMLNode:

if (rootElement.hasChildNodes()) {
NodeList nodeList = rootElement.getChildNodes();
iterateNodeList(rootElement, nodeList);
}

If a node is of type element, the tag name of the element may be retrieved. Node type is obtained with the getNodeType() method, which returns a short value. The Node interface provides static fields for different types of nodes. The different types of nodes in an XML document are listed in the following table:

Node Type

Description

ELEMENT_NODE

Element node.

ATTRIBUTE_NODE

Attribute node.

TEXT_NODE

Text node, for example the text in an element such as <elementA>Element A Text</elementA>.

CDATA_SECTION_NODE

CDATA section node. We discussed a CDATA section in an earlier table.

ENTITY_REFERENCE_NODE

Entity reference node. An entity reference refers to the content of a named entity.

ENTITY_NODE

Entity node. An entity is defined in a DOCTYPE declaration or an external DTD, and represents an abbreviation for data that is to be used repeatedly.

PROCESSING_INSTRUCTION_NODE

Processing Instruction node. We discussed a processing instruction in an earlier section.

COMMENT_NODE

Comment node. We discussed a comment node in an earlier section.

DOCUMENT_NODE

Document node. The document node represents the complete DOM document tree.

DOCUMENT_TYPE_NODE

Doctype node represents the DOCTYPE declaration.

DOCUMENT_FRAGMENT_NODE

DocumentFragment node. A document fragment is a segment of a document.

NOTATION_NODE

Notation node. A notation is defined in a DOCTYPE declaration or an external DTD. Notations represent the format of unparsed entities (non-XML data that a parser does not parse), format of elements with a notation attribute, and the application to which a processing instruction is sent. An example of a notation is as follows:

<!NOTATION gif PUBLIC "gif viewer">

For an element node, cast the node to XMLElement and output the element tag name:

if (node.getNodeType() == XMLNode.ELEMENT_NODE) {
XMLElement element = (XMLElement) node;
System.out.println("Element Tag Name:"+
element.getTagName))
}

The attributes in a element node are retrieved with the getAttributes() method, which returns a NamedNodeMap of attributes. The getLength() method of NamedNodeMap returns the length of an attribute node list. The method item(int) returns an Attr object for the attribute at the specified index. As class XMLAttr implements the Attr interface, cast the Attr object to XMLAttr. Iterate over the NamedNodeMap to output the attribute name and value. The hasAttributes() method tests if an element node has attributes:

if (element.hasAttributes()) {
NamedNodeMap attributes = element.getAttributes();
for (int i = 0; i < attributes.getLength(); i++) {
XMLAttr attribute = (XMLAttr)attributes.item(i);
System.out.println(" Attribute: " + attribute.getName() +
" with value " +attribute.getValue());
}
}

Running the Java application

The complete DOMParserApp.java Java application code listing is listed as follows with notes about the different sections in the Java class:

  1. 1. First, we add the package and import statements.

    package xmlparser;
    import java.io.*;
    import oracle.xml.jaxp.*;
    import oracle.xml.parser.v2.*;
    import javax.xml.parsers.ParserConfigurationException;
    import org.w3c.dom.*;
    import org.xml.sax.SAXException;
    
  2. 2. Next, we add Java class DOMParserApp.

    public class DOMParserApp {
    
  3. 3. Then, we add the parseXMLDocument method to parse an XML document.

    public void parseXMLDocument() {
    try {
    
  4. 4. Now, we create the XMLDocument object by parsing the XML document catalog.xml.

    JXDocumentBuilderFactory factory = (JXDocumentBuilderFactory) JXDocumentBuilderFactory.newInstance();
    factory.setAttribute(JXDocumentBuilderFactory.ERROR_STREAM,
    new FileOutputStream(new File("c:/output/errorStream.txt")));
    factory.setAttribute(JXDocumentBuilderFactory.SHOW_WARNINGS,
    Boolean.TRUE);
    JXDocumentBuilder documentBuilder = (JXDocumentBuilder) factory.newDocumentBuilder();
    InputStream input = new FileInputStream(new File("catalog.xml"));
    XMLDocument xmlDocument =
    (XMLDocument)(documentBuilder.parse(input));
    
  5. 5. Here, we output the document character encoding, the XML version, and namespace node values from the parsed XML document.

    System.out.println("Encoding: " + xmlDocument.getEncoding());
    System.out.println("Version: " + xmlDocument.getVersion());
    NodeList namespaceNodeList = xmlDocument.getElementsByTagNameNS ("http://xdk.com/catalog/journal", "title");
    for (int i = 0; i < namespaceNodeList.getLength(); i++) {
    XMLElement namespaceElement =
    (XMLElement)namespaceNodeList.item(i);
    System.out.println("Namespace Prefix: " + namespaceElement. getNamespaceURI());
    System.out.println("Namespace URI: " + namespaceElement. getPrefix());
    System.out.println("Element Name: " + namespaceElement. getTagName());
    System.out.println("Element text: " + namespaceElement.getFirstChild().getNodeValue());
    }
    
  6. 6. Next, we obtain the subnodes of the root element and invoke the iterateNodeList method to iterate over the subnodes.

    XMLElement rootElement =
    (XMLElement)(xmlDocument.getDocumentElement());
    System.out.println("Root Element is: " + rootElement.getTagName());
    if (rootElement.hasChildNodes()) {
    NodeList nodeList = rootElement.getChildNodes();
    iterateNodeList(rootElement, nodeList);
    }
    }
    catch (ParserConfigurationException e) {
    System.err.println(e.getMessage());
    } catch (FileNotFoundException e) {
    System.err.println(e.getMessage());
    } catch (IOException e) {
    System.err.println(e.getMessage());
    } catch (SAXException e) {
    System.err.println(e.getMessage());
    }
    }
    
  7. 7. The iterateNodeList method has an Element parameter, which represents the element with subnodes. The second parameter is of the type NodeList, which is the NodeList of subnodes of the Element represented by the first parameter.

    public void iterateNodeList(Element elem, NodeList nodeList) {
    if (nodeList.getLength() > 1) {
    System.out.println("Element " + elem.getTagName() +
    " has sub-elements\n");
    }
    
  8. 8. Iterate over the NodeList.

    for (int i = 0; i < nodeList.getLength(); i++) {
    XMLNode node = (XMLNode)nodeList.item(i);
    
  9. 9. If a node is of type Element, output the Element tag name and element text.

    if (node.getNodeType() == XMLNode.ELEMENT_NODE) {
    XMLElement element = (XMLElement)node;
    System.out.println("Sub-element of " + elem.getNodeName());
    System.out.println("Element Tag Name:" + element.getTagName());
    System.out.println("Element text: " + element.getFirstChild().getNodeValue());
    
  10. 10. If an Element has attributes, output the attributes.

    if (element.hasAttributes()) {
    System.out.println("Element has attributes\n");
    NamedNodeMap attributes = element.getAttributes();
    for (int j = 0; j < attributes.getLength(); j++) {
    XMLAttr attribute = (XMLAttr)attributes.item(j);
    System.out.println("Attribute: " +attribute.getName() +
    " with value "+ attribute.getValue());
    }
    }
    
  11. 11. If an Element has subnodes, obtain the NodeList of subnodes and iterate over the NodeList by invoking the iterateNodeList method again.

    if (element.hasChildNodes()) {
    iterateNodeList(element, element.getChildNodes());
    }
    }
    }
    }
    
  12. 12. Finally, we add the main method. In the main method, we create an instance of the DOMParserApp class and invoke the parseXMLDocument method.

    public static void main(String[] argv) {
    DOMParserApp domParser = new DOMParserApp();
    domParser.parseXMLDocument();
    }
    }
    
  13. 13. To run the DOMParserApp.java in JDeveloper, right-click on the DOMParserApp.java node in Application Navigator and select Run.

  14. 14. The element and attribute values from the XML document get outputted.

    The complete output from the DOM parsing application is as follows:

    Encoding: UTF-8
    Version: 1.0
    Namespace Prefix: http://xdk.com/catalog/journal
    Namespace URI: journal
    Element Name: journal:title
    Element text: Declarative Data Filtering
    Root Element is: catalog
    Element catalog has sub-elements
    Sub-element of catalog
    Element Tag Name:journal:journal
    Element text:
    Element has attributes
    Attribute: journal:title with value Oracle Magazine
    Attribute: journal:publisher with value Oracle Publishing
    Attribute: journal:edition with value March-April 2008
    Attribute: xmlns:journal with value http://xdk.com/catalog/journal
    Element journal:journal has sub-elements
    Sub-element of journal:journal
    Element Tag Name:journal:article
    Element text:
    Element has attributes
    Attribute: journal:section with value Oracle Developer
    Element journal:article has sub-elements
    Sub-element of journal:article
    Element Tag Name:journal:title
    Element text: Declarative Data Filtering
    Sub-element of journal:article
    Element Tag Name:journal:author
    Element text: Steve Muench
    Sub-element of catalog
    Element Tag Name:journal
    Element text:
    XML document parsing, DOM API usedDOM parsing application outputElement has attributes
    Attribute: title with value Oracle Magazine
    Attribute: publisher with value Oracle Publishing
    Attribute: edition with value September-October 2008
    Element journal has sub-elements
    Sub-element of journal
    Element Tag Name:article
    Element text:
    Element has attributes
    Attribute: section with value FEATURES
    Element article has sub-elements
    Sub-element of article
    Element Tag Name:title
    Element text: Share 2.0
    Sub-element of article
    Element Tag Name:author
    Element text: Alan Joch
    

To demonstrate error handling with the ERROR_STREAM attribute, add an error in the example XML document. For example, remove a </journal> tag. Run the DOMParserApp.java application in JDeveloper. An error message gets outputted to the file specified in the ERROR_STREAM attribute:

<Line 15, Column 10>: XML-20121: (Fatal Error) End tag
does not match start tag 'journal'.

 

Parsing an XML document with the SAX API


SAX parsing is based on the push model in which events are generated by an SAX parser and a document handler receives notification of the events. The SAX parsing model is faster than DOM parsing, but is limited in its scope to generating parsing events, without any provision for navigating the nodes or retrieving nodes with XPath. In this section, we shall parse the example XML document with a SAX parser and output the events generated by the parser. The SAX parsing application will be developed in JDeveloper in the Java application SAXParserApp.java. First, import the oracle.xml.jaxp package:

import oracle.xml.jaxp.*;

An SAX parsing application typically extends the DefaultHandler class, which has event-notification methods for the parse events. The DefaultHandler class implements the ErrorHandler interface. A DefaultHandler object may also be used for error handling.

Creating the factory

Create a JXSAXParserFactory object with the static method newInstance(). The newInstance() method returns a SAXParserFactory object that may be cast to JXSAXParserFactory as the JXSAXParserFactory class extends the SAXParserFactory class. Why cast, you might ask? We are using Oracle XDK 11g's implementation classes for various standard interfaces and abstract classes.

JXSAXParserFactory factory = (JXSAXParserFactory) JXSAXParserFactory.newInstance();

The factory object is used to obtain a SAX parser that may be used to parse an XML document.

Parsing the XML document

Create a SAXParser object from the factory object with the newSAXParser() method. As class JXSAXParser extends the SAXParser class, a SAXParser object may be cast to JXSAXParser:

JXSAXParser saxParser = (JXSAXParser) factory.newSAXParser();

Create an InputStream for the XML document to parse, and parse the XML document with one of the parse() methods in the SAXParser class. The parse methods take an XML document in the form of InputSource, InputStream, URI, or File, and an event handler such as the DefaultHandler.

InputStream input = new FileInputStream(new File("catalog.xml"));
saxParser.parse(input, this);

The DefaultHandler class provides the parsing event notification methods and error handling methods. Event notification and error handling methods may be overridden in the SAX parsing application for application-specific events and error handling. Some of the event notification methods in the DefaultHandler class are listed in the following table:

Method Name

Description

startDocument()

Receive notification of the start of the document. The startDocument method is invoked before the XML declaration.

endDocument()

Receive notification of the end of the document. The endDocument method is invoked after the closing tag of the root element.

startElement(java.lang.String uri, java.lang.String localName, java.lang.String qName, Attributes attributes)

Receive notification of the start of an element. The URI parameter specifies the namespace URI. LocalName specifies the element local name, which is the element name without the prefix. QName specifies the qualified element name; element name with prefix. The attributes parameter specifies the list of attributes in an element.

endElement(java.lang.String uri, java.lang.String localName, java.lang.String qName)

Receive notification of the end of an element.

characters(char[] ch, int start, int length)

Receive notification of the character data (text).

ignorableWhitespace(char[] ch, int start, int length)

Receive notification of the ignorable whitespace in an element.

notationDecl(String name, String publicId, String systemId)

Receive notification of a notation.

processingInstruction(String target, String data)

Receive notification of a processing instruction.

unparsedEntityDecl(String name, String publicId, String systemId, String notationName)

Receive notification of an unparsed entity declaration. Unparsed entity declarations are entities that refer to non-XML data that a parser does not have to parse. For example:

<!ENTITY banner SYSTEM "http://www.logos.net/logo.jpg" NDATA jpeg>

<!NOTATION jpeg PUBLIC "jpeg viewer">

skippedEntity(String name)

Receive notification of a skipped entity. Skipped entity notifications may be received when using a non-validating parser, which is not required to parse an external DTD and thus not required to resolve all entity references. Entity references that are not resolved are skipped entities.

startPrefixMapping(String prefix, String uri)

Receive notification of the start of a namespace mapping. An example of a namespace mapping is as follows:

xmls:xsd="http://www.w3.org/2001/XMLSchema"

endPrefixMapping(String prefix)

Receive notification of the end of a namespace mapping.

In the SAXParserApp.java application, some of the notification methods are overridden to output the event type, element name, element attributes, and element text. For example, the attributes represented by the Attributes object in the startElement(java.lang.String uri, java.lang.String localName, java.lang.String qName, Attributes atts) method may be iterated to output the attribute name, namespace URI, and attribute value:

for (int i = 0; i < atts.getLength(); i++) {
System.out.println("Attribute QName:" +atts.getQName(i));
System.out.println("Attribute Local Name:"+ atts.getLocalName(i));
System.out.println("Attribute Namespace URI:" + atts.getURI(i));
System.out.println("Attribute Value:"+atts.getValue(i));
}

The error handler methods in DefaultHandler may also be overridden. In the SAXParserApp.java application, the error handler methods are overridden to output the error message. We shall demonstrate error handling in a SAX parsing application with an example. Error handler methods in the DefaultHandler class are listed in the following table:

Method Name

Description

error(SAXParseException exception)

Receives notification of a recoverable error

fatalError(SAXParseException exception)

Receives notification of a non-recoverable error

warning(SAXParseException exception)

Receives notification of a warning

Running the Java application

The SAXParserApp.java application is listed as follows with notes about the different sections of the Java class:

  1. 1. Add the package and import statements.

    package xmlparser;
    import oracle.xml.jaxp.*;
    import org.xml.sax.Attributes;
    import org.xml.sax.SAXException;
    import org.xml.sax.SAXParseException;
    import org.xml.sax.helpers.DefaultHandler;
    import java.io.*;
    import javax.xml.parsers.ParserConfigurationException;
    
  2. 2. Next, we add the Java class SAXParserApp.

    public class SAXParserApp extends DefaultHandler {
    
  3. 3. Next, we add a method parseXMLDocument to parse the XML document, catalog.xml.

    public void parseXMLDocument() {
    try {
    JXSAXParserFactory factory = (JXSAXParserFactory) JXSAXParserFactory.newInstance();
    JXSAXParser saxParser = (JXSAXParser) factory.newSAXParser();
    InputStream input = new FileInputStream(new File("catalog.xml"));
    saxParser.parse(input, this);
    }catch (ParserConfigurationException e) {
    System.err.println(e.getMessage());
    }catch (FileNotFoundException e) {
    System.err.println(e.getMessage());
    }catch (IOException e) { System.err.println(e.getMessage());
    }catch (SAXException e) { System.err.println(e.getMessage());
    }
    }
    
  4. 4. The startDocument event notification method notifies about the start of an XML document.

    public void startDocument() throws SAXException {
    System.out.println("SAX Event : Start Document");
    }
    
  5. 5. The endDocument event notification method notifies about the end of an XML document.

    public void endDocument() throws SAXException {
    System.out.println("SAX Event : End Document");
    }
    
  6. 6. The startElement event notification method notifies about the start of an element.

    public void startElement(java.lang.String namespaceURI,
    java.lang.String localName, java.lang.String qName, Attributes atts)throws SAXException {
    System.out.println("SAX Event : Start Element");
    System.out.println("Element QName:" + qName);
    System.out.println("Element Local Name:" + localName);
    System.out.println("Element Namespace URI:"+namespaceURI);
    for (int i = 0; i < atts.getLength(); i++) {
    System.out.println("Attribute QName:"+atts.getQName(i));
    System.out.println("Attribute Local Name:"+ atts.getLocalName(i));
    System.out.println("Attribute Namespace URI:"+ atts.getURI(i));
    System.out.println("Attribute Value:"+atts.getValue(i));
    }
    }
    
  7. 7. The event notification method endElement notifies about the end of an element.

    public void endElement(java.lang.String namespaceURI,
    java.lang.String localName, java.lang.String qName)
    throws SAXException {
    System.out.println("SAX Event : End Element");
    System.out.println("Element QName:" + qName);
    }
    
  8. 8. The event notification method characters notifies about character text.

    public void characters(char[] ch, int start, int length)
    throws SAXException {
    System.out.println("SAX Event : Text");
    String text = new String(ch, start, length).trim();
    if (text.length() > 0) {
    System.out.println("Text:" + text);
    }
    }
    
  9. 9. Here, the error handling method error handles recoverable errors.

    public void error(SAXParseException e) throws SAXException {
    System.err.println("Error:" + e.getMessage());
    }
    
  10. 10. The error handling method fatalError handles non-recoverable errors.

    public void fatalError(SAXParseException e) throws SAXException {
    System.err.println("Fatal Error:" + e.getMessage());
    }
    
  11. 11. The error handling method warning handles parser warnings.

    public void warning(SAXParseException e) throws SAXException {
    System.out.println("Warning:" + e.getMessage());
    }
    
  12. 12. Finally, add the main method. In the main method, create an instance of the SAXParserApp class and invoke the parseXMLDocument method.

    public static void main(String[] argv) {
    SAXParserApp saxParser = new SAXParserApp();
    saxParser.parseXMLDocument();
    }
    }
    
    
  13. 13. To run the SAXParserApp.java application in JDeveloper, right-click on the SAXParserApp.java node in Application Navigator, and select Run.

The output from the SAX parsing application lists the SAX events, elements, and attributes in the parsed XML document.

The complete output from the SAX parsing application is listed as follows:

SAX Event : Start Document
SAX Event : Start Element
Element QName:catalog
Element Local Name:catalog
Element Namespace URI:
SAX Event : Text
SAX Event : Start Element
Element QName:journal:journal
Element Local Name:journal
Element Namespace URI:http://xdk.com/catalog/journal
Attribute QName:journal:title
Attribute Local Name:title
Attribute Namespace URI:http://xdk.com/catalog/journal
Attribute Value:Oracle Magazine
Attribute QName:journal:publisher
Attribute Local Name:publisher
Attribute Namespace URI:http://xdk.com/catalog/journal
Attribute Value:Oracle Publishing
Attribute QName:journal:edition
Attribute Local Name:edition
Attribute Namespace URI:http://xdk.com/catalog/journal
Attribute Value:March-April 2008
Attribute QName:xmlns:journal
Attribute Local Name:journal
Attribute Namespace URI:http://www.w3.org/2000/xmlns/
Attribute Value:http://xdk.com/catalog/journal
SAX Event : Text
SAX Event : Start Element
Element QName:journal:article
Element Local Name:article
Element Namespace URI:http://xdk.com/catalog/journal
Attribute QName:journal:section
Attribute Local Name:section
Attribute Namespace URI:http://xdk.com/catalog/journal
Attribute Value:Oracle Developer
SAX Event : Text
SAX Event : Start Element
Element QName:journal:title
Element Local Name:title
Element Namespace URI:http://xdk.com/catalog/journal
SAX Event : Text
Text:Declarative Data Filtering
SAX Event : End Element
Element QName:journal:title
SAX Event : Text
SAX Event : Start Element
Element QName:journal:author
Element Local Name:author
Element Namespace URI:http://xdk.com/catalog/journal
SAX Event : Text
Text:Steve Muench
SAX Event : End Element
Element QName:journal:author
SAX Event : Text
SAX Event : End Element
Element QName:journal:article
SAX Event : Text
SAX Event : End Element
Element QName:journal:journal
SAX Event : Text
SAX Event : Start Element
Element QName:journal
Element Local Name:journal
Element Namespace URI:
Attribute QName:title
Attribute Local Name:title
Attribute Namespace URI:
Attribute Value:Oracle Magazine
Attribute QName:publisher
Attribute Local Name:publisher
Attribute Namespace URI:
Attribute Value:Oracle Publishing
Attribute QName:edition
Attribute Local Name:edition
Attribute Namespace URI:
Attribute Value:September-October 2008
SAX Event : Text
SAX Event : Start Element
Element QName:article
Element Local Name:article
Element Namespace URI:
Attribute QName:section
Attribute Local Name:section
Attribute Namespace URI:
Attribute Value:FEATURES
SAX Event : Text
SAX Event : Start Element
Element QName:title
Element Local Name:title
Element Namespace URI:
SAX Event : Text
Text:Share 2.0
SAX Event : End Element
Element QName:title
SAX Event : Text
SAX Event : Start Element
Element QName:author
Element Local Name:author
Element Namespace URI:
SAX Event : Text
Text:Alan Joch
SAX Event : End Element
Element QName:author
SAX Event : Text
SAX Event : End Element
Element QName:article
SAX Event : Text
SAX Event : End Element
Element QName:journal
SAX Event : Text
SAX Event : End Element
Element QName:catalog
SAX Event : End Document

To demonstrate error handling, add an error in the example XML document. For example, remove a </journal> node, as we did earlier. Run the SAXParserApp.java application in JDeveloper. An error message gets outputted:

Fatal Error:<Line 15, Column 10>: XML-20121: (Fatal Error) End tag does not match start tag 'journal'.
 

Summary


In this chapter we demonstrated the DOM and SAX approaches to parsing an XML document. DOM parsing is suitable if document nodes are to be modified and navigated with random access. SAX parsing is useful if you want to go through the XML document once, responding to nodes as you encounter them. If you want to create an encapsulation of the XML document as a DOM tree that you can manipulate later, you need to use DOM parsing.

In this chapter we created an XML document using the JAXP API. In the next chapter, we will create an XML document from an XML Schema. First, we will create an XML Schema in JDeveloper and subsequently we will instantiate an XML document from the XML Schema.

Processing XML documents with Oracle JDeveloper 11g
Unlock this book and the full library FREE for 7 days
Start now