-
Book Overview & Buying
-
Table Of Contents
Processing XML documents with Oracle JDeveloper 11g
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 the createElementNS(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 the appendChild method.
catalogElement.appendChild(journalElement);
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. 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. 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 |
|---|---|
|
|
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 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. 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 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. 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. 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 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();
}
}
Change the font size
Change margin width
Change background colour