Processing XML documents with Oracle JDeveloper 11g
上QQ阅读APP看书,第一时间看更新

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:

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:

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

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:

Running the Java application

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

Running the Java application

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

Running the Java application

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