Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Processing XML documents with Oracle JDeveloper 11g
Processing XML documents with Oracle JDeveloper 11g

Processing XML documents with Oracle JDeveloper 11g:

eBook
R$49.99 R$271.99
Paperback
R$339.99
Subscription
Free Trial
Renews at R$50p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Processing XML documents with Oracle JDeveloper 11g

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.

Left arrow icon Right arrow icon

Key benefits

  • Will get the reader developing applications for processing XML in JDeveloper 11g quickly and easily
  • Self-contained chapters provide thorough, comprehensive instructions on how to use JDeveloper to create, validate, parse, transform, and compare XML documents.
  • The only title to cover XML processing in Oracle JDeveloper 11g, this book includes information on the Oracle XDK 11g APIs.
  • Packed with example code and detailed commentary, the book is fully illustrated with functional step-by-step examples.

Description

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.

Who is this book for?

Employing a comprehensive tutorial-based approach, this easy-to-follow book shows the reader various means of processing XML documents using the power of Oracle's JDeveloper 11g. In next to no time, the reader will be able to create, format, transform, compare, and schema validate XML documents with Oracle's IDE.

What you will learn

  • Learn what JDeveloper 11g can do for XML document generation.
  • Rapidly create, format, compare, and schema validate XML documents.
  • Master the built-in XML features in JDeveloper 11g including schema validation, the XSD Visual Editor for creating an XML schema, the XPath Search tool for selecting XML nodes with XPath, and the JAXB compiler for generating JAXB 2.0 Content Model from an XML Schema.
  • See how to work quickly and efficiently with the Oracle XML Developer Kit (XDK 11g)
  • Gain a valuable understanding of JAXP, XPath, XSLT, JAXB 2.0, DOM 3.0, and the JSTL XML Tag Library.
  • Convert XML to Excel and PDF formats, store XML in Oracle Berkeley DB XML, and create Oracle XML Publisher Reports.

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : May 15, 2009
Length: 384 pages
Edition : 1st
Language : English
ISBN-13 : 9781847196675
Vendor :
Oracle
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : May 15, 2009
Length: 384 pages
Edition : 1st
Language : English
ISBN-13 : 9781847196675
Vendor :
Oracle
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
R$50 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
R$500 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just R$25 each
Feature tick icon Exclusive print discounts
R$800 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just R$25 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total R$ 707.98
Processing XML documents with Oracle JDeveloper 11g
R$339.99
Oracle Advanced PL/SQL Developer Professional Guide
R$367.99
Total R$ 707.98 Stars icon
Banner background image

Table of Contents

14 Chapters
Creating and Parsing an XML Document Chevron down icon Chevron up icon
Creating an XML Schema Chevron down icon Chevron up icon
XML Schema Validation Chevron down icon Chevron up icon
XPath Chevron down icon Chevron up icon
Transforming XML with XSLT Chevron down icon Chevron up icon
JSTL XML Tag Library Chevron down icon Chevron up icon
Loading and Saving XML with DOM 3.0 LS Chevron down icon Chevron up icon
Validating an XML Document with DOM 3 Validation Chevron down icon Chevron up icon
JAXB 2.0 Chevron down icon Chevron up icon
Comparing XML Documents Chevron down icon Chevron up icon
Converting XML to PDF Chevron down icon Chevron up icon
Converting XML to MS Excel Chevron down icon Chevron up icon
Storing XML in Oracle Berkeley DB XML Chevron down icon Chevron up icon
Oracle XML Publisher Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.2
(5 Ratings)
5 star 40%
4 star 40%
3 star 20%
2 star 0%
1 star 0%
SOA Apr 02, 2010
Full star icon Full star icon Full star icon Full star icon Full star icon 5
processing XML document with Jdeveloper 11g is very book. I wanted to know all the features in Jdeveloper (wizards) that one would use in creating, processing XML, XSD, XPATH, XSLThis book really shows the illustrations in a simple step by step manner. I highly recommend anyone who want to FAST TRACK there XML, XSD, XPATH skills and use it in SOA, OR WebServices. 5 STARS.
Amazon Verified review Amazon
Suresh Krishna May 23, 2009
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Oracle XML Developer Kit (XDK) is a set of components, tools and utilities in Java which is available in Oracle Database, Oracle Application Server and on OTN that eases the task of building and deploying XML-enabled applications with a commercial redistribution license. XDK has several useful components such as XML Parsers, XSLT Processors, XSLT VM, XML Schema Processors, XML Java Beans, XML Class Generator, XML SQL Utility and XSQL Servlet.Oracle JDeveloper provides a rich set of tools and utilities for the XML processing. JDeveloper includes the XDK and any developer can make use of its powerful features to develop any XML based applications. Processing XML Documents with Oracle JDeveloper is a great book and i would highly recommend for everyone working with XDK, and JDeveloper.The author Deepak Vohra did a good job in describing the various steps for the XML Processing, XML Schema Validation, XPath Support, XML and XSLT Transformations. For more advanced users of XML, this book also talks about the topics such as Java XML Binding (JXB), API for comparing the documents, Converting XML to PDF, Converting XML to MS Excel, and finally storing the XML in Oracle Berkley DB XML.This book provides a quick reference guide to any developer who starts their XDK development using JDeveloper. Author emphasized well enough about the small details that every developer should know in XML processing. Author gives a good introduction about all the technologies that he talks and mentions the APIs in detail. I really liked the part where the author gives a complete java source code (in almost each chapter) along with the comments to explain the purpose of the following code. This book did a good job in setting up the context for running the applications. Author describes the steps to setup the extra environment variables to bet set, jars to be added and how to navigate in JDeveloper. With this sort of detailing, any developer could just look at the book and will be able to start the development right away. Also, the publisher, Packt extracted chapter (Chapter 4: XPath) from the book and you can find it here: [...]I would definitely recommend this book for the XML developers using XDK and JDeveloper.
Amazon Verified review Amazon
Serafeim Karapatis Aug 31, 2009
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
The books that concern Oracle JDeveloper are very limited and this one really worth studying. The chapters are roughly organized in three major areas:- The Oracle XDK API capabilities for processing XML documents- The XML tools and visual editors that JDeveloper provides- The description of standardized or open-source XML libraries (such as those of Apache) that are commonly used in Java projects.All examples in the above areas are very well explained and are described in detail for the environment of JDeveloper 11g. In summary, the pace of the book is easy to follow, especially from a beginner perspective and the abundance of examples make it a perfect match for a reference book.[...]
Amazon Verified review Amazon
P. Barut Mar 29, 2009
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
I would like to recommend a book Processing XML documents with Oracle JDeveloper 11g published by PacktPub.com. As I have some experience with XML processing inside Oracle DB (XMLDB) I was lacking the knowledge of XML manipulation in Java, especially using Oracle JDeveloper. This book is rather not for beginners - you should have basic knowledge of what is XML, at least theoretically. From the book you can learn how to manage XML Schemas in JDeveloper, then write XML documents based on those schema and finally validate them in JDeveloper or write Java code to validate it automatically. Chapters that I value much are those describing how to generate PDF documents. There are two methods described - one using Apache FOP API , and Oracle XML Publisher. As I was not generating PDF's this way so far, I found those 2 chapters very interesting. There is also chapter on generatin Excel by transformation of XML using Apache HSSF API .Book is very practical. If you want to start with subjects described above then this book is for you. Of course it does not mean that you do not have to study documentation. However, it will be easier for you to make first steps. Beside describing Oracle JDeveloper, author also shows how to install and use Oracle Berkley XML DB to store, query and update XML Documents - using command line and XML DB API.I especially recommend eBook version. All URLs in eBook version are active, so linking to web pages are very simple. Also coping sample code is easy. This book contains many practical and useful tips on XML processing and tools as well. So if those XML aspects are in your interest, then it's definitely good to invest in this book./Pawel
Amazon Verified review Amazon
KM Jun 02, 2009
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
Recently I have been approached by Packt Publishing to review their recently released book "Processing XML documents with Oracle JDeveloper 11g" by Deepak Vohra. They were kind enough to send me an e-book, which suits more to a person like me who lives out of suitcase.Coming back to the point, as the book rightly mentioned, its objective is "to discuss XML development in Oracle JDeveloper" especially around JDeveloper 11g. JDeveloper has matured into a very robust IDE especially in J2EE and SOA arena. It's XML handling capabilities are worthy enough to be mentioned separately. In a way Gaurav did a very smart job of focusing on one topic rather than try to cover wider topics in shallow waters.First six chapters covers the basic of XML document, DOM, SAX, XSD, XSLT, XPath, JSTL XML taglibs. It was little bit an over-stretch for me, but I wouldn't discount it completely as it can be really helpful for somebody who is really starting from scratch (truly no exemption). Most of the examples covered in these chapters can be easily mapped to other parsers without much difficulties. Chapter seven, eight and nine are amongst the ones which interested me most. I like theory portion of DOM 3.0 LS.Chapter 10, 11, 12, 13 and 14 covers interesting and useful applied XML technologies like XMLDiff, XML2PDF, XML2XLS, Berkeley and XML Publisher. Chapters cover enough to give you a start but to deal with real life scenarios you might have to dig deeper. All in all these chapters might excite those who really like to try something new and different.Overall it's an easy read book which explains XML fundamentals in easy to understand language. I would definitely like this book as a beginner but it doesn't have a lot to offer at intermediate level. I would have like to see some literature around SDOM, AJAX, StAX. All in all this is a good beginners book covering XML and related technologies.[...]
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.