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.
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.

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.

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.*;
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 |
DEBUG_MODE |
Specifies the debug mode. The debug mode value is a Boolean and value may be set to |
DTD_OBJECT |
Specifies the DTD object to validate the XML document. The DTD object is set as an |
ERROR_ENCODING |
Specifies the text encoding for error reports in the error stream. Error encoding is specified as a string literal such as |
ERROR_STREAM |
Specifies the error stream for reporting errors. The attribute value can be an |
NODE_FACTORY |
Specifies the |
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 |
SCHEMA_SOURCE |
Specifies the XML Schema file to be used for validation. Its value may be set to one of the following:
|
SHOW_WARNINGS |
Specifies if warnings are to be shown during schema validation. Its value is a Boolean, which may be set to |
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 |
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 |
---|---|
|
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 |
|
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. |
|
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 |
|
Gets document character encoding. The document encoding is specified in the encoding parameter. For example, |
|
Sets document character encoding. The document encoding gets set as the "encoding" parameter in the XML declaration when the document is saved. |
|
Prints the contents of the DOM tree using the specified |
|
Prints the contents of the external DTD using the specified |
|
Sets the doctype for the document. When the DOM tree is saved, the |
|
Returns the XML version. The XML version is specified in the "version" parameter of the XML declaration, for example |
|
Sets XML version. |
|
Returns the |
|
Sets |
|
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. |
|
Gets the standalone parameter value for the XML declaration. |
|
Sets the locale for error reporting. For example, for the UK you would create the Locale object as:
|
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");
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);
Next, we shall create the DOM document tree:
1. Create the namespace element
journal:journal
with thecreateElementNS(String, String)
method.XMLElement journalElement = (XMLElement) (xmlDocument.createElementNS("http://xdk.com/catalog/journal","journal:journal"));
2. Add the
journal
element to the root element using theappendChild
method.catalogElement.appendChild(journalElement);
3. Add a namespace attribute
journal:title
with thesetAttributeNS(String, String, String)
method.journalElement.setAttributeNS("http://xdk.com/catalog/journal", "journal:title", "Oracle Magazine");
4. Similarly, add
journal:publisher
andjournal:author
attributes. Addjournal:article
andjournal:title
elements similar to thejournal:journal
element. Create anXMLText
node, which represents a text node, to set the text of thetitle
element using thecreateTextNode(String)
method.XMLText title = (XMLText) xmlDocument.createTextNode ("Declarative Data Filtering");
5. Add the
XMLText
node tojournal:title
element using theappendChild
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 |
---|---|
|
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. |
|
Creates a comment. Comments are represented with |
|
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. |
|
Creates a Processing Instruction. Processing Instructions are not included in a document's character data and are instructions for the application. |
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 |
---|---|
|
Prints an attribute node. |
|
Prints attributes in an element node. |
|
Prints a CDATA section node. |
|
Prints child nodes for a node. |
|
Prints comment node. |
|
Prints DTD. |
|
Prints a document. We used this method in outputting the DOM document tree. |
|
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. |
|
Prints an element node. |
|
Prints an entity reference node. |
|
Prints a processing instruction node. |
|
Prints a text node. |
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. First, we declare the
package
statement and theimport
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. Next, we define the Java class
CreateXMLDocument
.public class CreateXMLDocument {
3. Now, we define the method to create an XML document.
public void createXMLDocument() { try {
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. Here, we create the root element
catalog
and the first subelementjournal
.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. Next, we add namespace attributes
title, publisher
, andedition
to thejournal
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. 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. 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. Finally, we define the
main
method for the Java class. In themain
method, we create an instance of theCreateXMLDocument
class and invoke thecreateXMLDocument
method.public static void main(String[] argv) { CreateXMLDocument createXMLDocument = new CreateXMLDocument(); createXMLDocument.createXMLDocument(); } }
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.*;
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:
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.
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 |
---|---|
|
Returns the root element. |
|
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. |
|
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. |
|
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 |
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:
|
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:
The complete DOMParserApp.java
Java application code listing is listed as follows with notes about the different sections in the Java class:
1. First, we add the
package
andimport
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. Next, we add Java class
DOMParserApp
.public class DOMParserApp {
3. Then, we add the
parseXMLDocument
method to parse an XML document.public void parseXMLDocument() { try {
4. Now, we create the
XMLDocument
object by parsing the XML documentcatalog.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. 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. 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. The
iterateNodeList
method has anElement
parameter, which represents the element with subnodes. The second parameter is of the typeNodeList
, which is theNodeList
of subnodes of theElement
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. Iterate over the
NodeList
.for (int i = 0; i < nodeList.getLength(); i++) { XMLNode node = (XMLNode)nodeList.item(i);
9. If a node is of type
Element
, output theElement
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. 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. If an
Element
has subnodes, obtain theNodeList
of subnodes and iterate over theNodeList
by invoking theiterateNodeList
method again.if (element.hasChildNodes()) { iterateNodeList(element, element.getChildNodes()); } } } }
12. Finally, we add the
main
method. In themain
method, we create an instance of theDOMParserApp
class and invoke theparseXMLDocument
method.public static void main(String[] argv) { DOMParserApp domParser = new DOMParserApp(); domParser.parseXMLDocument(); } }
13. To run the
DOMParserApp.java
in JDeveloper, right-click on the DOMParserApp.java node in Application Navigator and select Run.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:
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.
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.
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 |
---|---|
|
Receive notification of the start of the document. The |
|
Receive notification of the end of the document. The |
|
Receive notification of the start of an element. The URI parameter specifies the namespace URI. |
|
Receive notification of the end of an element. |
|
Receive notification of the character data (text). |
|
Receive notification of the ignorable whitespace in an element. |
|
Receive notification of a notation. |
|
Receive notification of a processing instruction. |
|
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:
|
|
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. |
|
Receive notification of the start of a namespace mapping. An example of a namespace mapping is as follows:
|
|
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 |
---|---|
|
Receives notification of a recoverable error |
|
Receives notification of a non-recoverable error |
|
Receives notification of a warning |
The SAXParserApp.java
application is listed as follows with notes about the different sections of the Java class:
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. Next, we add the Java class
SAXParserApp
.public class SAXParserApp extends DefaultHandler {
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. 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. 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. 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. 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. 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. Here, the error handling method
error
handles recoverable errors.public void error(SAXParseException e) throws SAXException { System.err.println("Error:" + e.getMessage()); }
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. The error handling method
warning
handles parser warnings.public void warning(SAXParseException e) throws SAXException { System.out.println("Warning:" + e.getMessage()); }
12. Finally, add the
main
method. In themain
method, create an instance of theSAXParserApp
class and invoke theparseXMLDocument
method.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'.
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.