Book Image

Processing XML documents with Oracle JDeveloper 11g

Book Image

Processing XML documents with Oracle JDeveloper 11g

Overview of this book

XML is an open standard for creating markup languages and exchanging structured documents and data over the Internet. JDeveloper 11g presents an effective, quick, and easy-to-use means of processing XML documents. Inspired by the author's previous XML articles for the Oracle community, this expanded hands-on tutorial guides newcomers and intermediate users through JDeveloper 11g and XML document development. It offers up-to-date information on working with the latest version of JDeveloper, and brand new information on JAXB 2.0 support in JDeveloper 11g. Filled with illustrations, explanatory tables, and comprehensive instructions, this book walks the reader through the wide assortment of JDeveloper's capabilities. Oracle's JDeveloper 11g is an Integrated Development Environment that provides a visual and declarative approach to application development. Over the course of 14 chapters, readers will get hands-on with JDeveloper as the comprehensive and self-contained tutorials provide clear instruction on the key XML tasks that JDeveloper can accomplish. Filled with practical information and illustrated examples, this book shows the reader how to create, parse, and store XML documents quickly, as well as providing step-by-step instructions on how to construct an XML schema and use the schema to validate an XML document. Oracle's XML Developer Kit (XDK) offers a set of components, tools, and utilities for developing XML-based applications, and developers will find the detailed XDK coverage invaluable. Later chapters are given over to using XPath, transforming XML with XSLT, and using the JSTL XML Tag Library. Moving through the book, a chapter on the JAXB 2.0 API shows you how to bind, marshal and unmarshal XML documents, before we finally delve into comparing XML documents, and converting them into PDF and Excel formats. In all, this book will enable the reader to gain a good and wide-ranging understanding of what JDeveloper has to offer for XML processing.
Table of Contents (19 chapters)
Processing XML documents with Oracle JDeveloper 11g
Credits
About the Author
About the Reviewers
Preface

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();
    }
    }