Skip Headers

Oracle9i XML Database Developer's Guide - Oracle XML DB
Release 2 (9.2)

Part Number A96620-02
Go to Documentation Home
Go to Book List
Book List
Go to Table of Contents
Go to Index
Go to Master Index
Master Index
Go to Feedback page

Go to previous page Go to next page
View PDF

Java API for XMLType

This chapter describes how to use XMLType in Java, including fetching XMLType data through JDBC.

Introducing Java DOM API for XMLType

Oracle XML DB supports the Java Document Object Model (DOM) API for XMLType. This is a generic API for client and server, for both XML schema-based and non- schema-based documents. It is implemented using the Java package oracle.xdb.dom.

To access XMLType data using JDBC use the class oracle.xdb.XMLType.

For XML documents that do not conform to any XML schema, you can use the Java DOM API for XMLType as it can handle any valid XML document.

See Also:

Oracle9i XML API Reference - XDK and Oracle XML DB

Java DOM API for XMLType

Java DOM API for XMLType handles all kinds of valid XML documents irrespective of how they are stored in Oracle XML DB. It presents to the application a uniform view of the XML document irrespective of whether it is XML schema-based or non- schema-based, whatever the underlying storage. Java DOM API works on client and server.

As discussed in Chapter 8, "PL/SQL API for XMLType", the Oracle XML DB DOM APIs are compliant with W3C DOM Level 1.0 and Level 2.0 Core Recommendation.

Accessing XML Documents in Repository

Oracle XML DB Resource API for Java API allows Java applications to access XML documents stored in the Oracle XML DB Repository. Naming conforms to the Java binding for DOM as specified by the W3C DOM Recommendation. Oracle XML DB Repository hierarchy can store both XML schema-based and non- schema-based documents.


Chapter 17, "Oracle XML DB Resource API for Java"

Accessing XML Documents Stored in Oracle9i Database (Java)

Oracle XML DB provides the following way (part of the Java Resource APIs) for Java applications to access XML data stored in a database:

Using JDBC

This is an SQL-based approach for Java applications for accessing any data in Oracle9i database, including XML documents in Oracle XML DB. Use the oracle.xdb.dom.XMLType class, createXML() method.

How Java Applications Use JDBC to Access XML Documents in Oracle XML DB

JDBC users can query an XMLType table to obtain a JDBC XMLType interface that supports all methods supported by the SQL XMLType data type. The Java (JDBC) API for XMLType interface can implement the DOM document interface.

Example 9-1 XMLType Java: Using JDBC to Query an XMLType Table

The following is an example that illustrates using JDBC to query an XMLType table:

import oracle.xdb.XMLType; 
   OraclePreparedStatement stmt = (OraclePreparedStatement) 
conn.prepareStatement("select e.poDoc from po_xml_tab e"); 
       ResultSet rset = stmt.executeQuery(); 
       OracleResultSet orset = (OracleResultSet) rset; 

       // get the XMLType 
       XMLType poxml = XMLType.createXML(orset.getOPAQUE(1)); 
       // get the XMLDocument as a string... 
Document podoc = (Document)poxml.getDOM(); 

Example 9-2 XMLType Java: Selecting XMLType Data

You can select the XMLType data in JDBC in one of two ways:

Example 9-3 XMLType Java: Directly Returning XMLType Data

This example shows the use of getObject to directly get the XMLType from the ResultSet. This is the easiest way to get the XMLType from the ResultSet.

import oracle.xdb.XMLType;

 OraclePreparedStatement stmt = 
    (OraclePreparedStatement) conn.prepareStatement(
          "select e.poDoc from po_xml_tab e");

ResultSet rset = stmt.executeQuery();
OracleResultSet orset = (OracleResultSet) rset;

// get the XMLType
XMLType poxml = (XMLType)orset.getObject(1);

// get the XML as a string...
String poString = poxml.getStringVal();

Using JDBC to Manipulate XML Documents Stored in a Database

You can also update, insert, and delete XMLType data using JDBC.

Example 9-4 XMLType Java: Updating/Inserting/Deleting XMLType Data

You can insert an XMLType in java in one of two ways:

Example 9-5 XMLType Java: Getting Metadata on XMLType

When selecting out XMLType values, JDBC describes the column as an OPAQUE type. You can select the column type name out and compare it with "XMLTYPE" to check if you are dealing with an XMLType:

import oracle.sql.*;
import oracle.jdbc.*;
OraclePreparedStatement stmt = 
    (OraclePreparedStatement) conn.prepareStatement(
        "select poDoc from po_xml_tab");

OracleResultSet rset = (OracleResultSet)stmt.exuecuteQuery();

// Now, we can get the resultset metadata
OracleResultSetMetaData mdata = 

// Describe the column = the column type comes out as OPAQUE
// and column type name comes out as XMLTYPE
if (mdata.getColumnType(1) == OracleTypes.OPAQUE && 
    mdata.getColumnTypeName(1).compareTo("SYS.XMLTYPE") == 0)
   // we know it is an XMLtype

Example 9-6 XMLType Java: Updating an Element in an XMLType Column

This example updates the discount element inside PurchaseOrder stored in an XMLType column. It uses Java (JDBC) and the oracle.xdb.XMLType class. This example also shows you how to insert/update/delete XMLTypes using Java (JDBC). It uses the parser to update an in-memory DOM tree and write the updated XML value to the column.

-- create po_xml_hist table to store old PurchaseOrders
create table po_xml_hist (
 xpo xmltype

    Example for oracle.xdb.XMLType

   Have, xmlparserv2.jar, and oraxdb.jar in CLASSPATH


import java.sql.*;

import oracle.xml.parser.v2.*;
import org.xml.sax.*;
import org.w3c.dom.*;

import oracle.jdbc.driver.*;
import oracle.sql.*;

import oracle.xdb.XMLType;

public class tkxmtpje

  static String conStr = "jdbc:oracle:oci8:@";
  static String user = "scott";
  static String pass = "tiger";
  static String qryStr = 
        "SELECT x.poDoc from po_xml_tab x "+
        "WHERE  x.poDoc.extract('/PO/PONO/text()').getNumberVal()=200";

 static String updateXML(String xmlTypeStr)
     String outXML = null;
        DOMParser parser  = new DOMParser();
        parser.setPreserveWhitespace (true);    

        parser.parse(new StringReader(xmlTypeStr));
        System.out.println("xmlType.getStringVal(): xml String is well-formed");

        XMLDocument doc = parser.getDocument();

        NodeList nl = doc.getElementsByTagName("DISCOUNT");

        for(int i=0;i<nl.getLength();i++){
           XMLElement discount = (XMLElement)nl.item(i);
           XMLNode textNode = (XMLNode)discount.getFirstChild();

       StringWriter sw = new StringWriter();
       doc.print(new PrintWriter(sw));

       outXML = sw.toString();

      //print modified xml
       System.out.println("Updated PurchaseOrder:");
    catch ( Exception e )
    return outXML;

 public static void main(String args[]) throws Exception

        System.out.println("qryStr="+ qryStr);

        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

        Connection conn = 
          DriverManager.getConnection("jdbc:oracle:oci8:@", user, pass);

        Statement s = conn.createStatement();
        OraclePreparedStatement stmt;

        ResultSet rset = s.executeQuery(qryStr);
        OracleResultSet orset = (OracleResultSet) rset;


        //retrieve PurchaseOrder xml document from database
         XMLType xt = XMLType.createXML(orset.getOPAQUE(1));
         //store this PurchaseOrder in po_xml_hist table
         stmt = (OraclePreparedStatement)conn.prepareStatement(
                   "insert into po_xml_hist values(?)");

         stmt.setObject(1,xt);  // bind the XMLType instance

         //update "DISCOUNT" element
         String newXML = updateXML(xt.getStringVal());

         // create a new instance of an XMLtype from the updated value
         xt = XMLType.createXML(conn,newXML);

        // update PurchaseOrder xml document in database
         stmt = (OraclePreparedStatement)conn.prepareStatement(
           "update po_xml_tab x set x.poDoc =? where "+

         stmt.setObject(1,xt);  // bind the XMLType instance

         System.out.println("PurchaseOrder 200 Updated!");


       //delete PurchaseOrder 1001
        s.execute("delete from po_xml x"+
           "where x.xpo.extract"+
        System.out.println("PurchaseOrder 1001 deleted!");
    catch( Exception e )

-- list PurchaseOrders

set long 20000
set pages 100
select x.xpo.getClobVal()
from po_xml x;

Here is the resulting updated purchase order in XML:

<?xml version = '1.0'?>
    <STREET>323 College Drive</STREET>
   <LINEITEM_TYP LineItemNo="1">
    <ITEM StockNo="1004">
   <LINEITEM_TYP LineItemNo="2">
    <ITEM StockNo="1011">
   <STREET>55 Madison Ave</STREET>

Example 9-7 Manipulating an XMLType Column

This example performs the following:

Java DOM API for XMLType Features

When you use the Java DOM API to get XML data from Oracle XML DB, you get an XMLDocument object that represents the XML data or file you retrieve. From this document interface you can get the elements of the document and perform all the operations specified in the W3C DOM specification. The DOM works on:

The Java DOM API for XMLType supports deep or shallow searching in the document to retrieve children and properties of XML objects such as name, namespace, and so on. Conforming to the DOM 2.0 recommendation, Java DOM API for XMLType is namespace aware.

Creating XML Documents Programmatically

Java API for XMLType also allows applications to create XML documents programmatically. This way applications can create XML documents on the fly (or dynamically) that either conform to a preregistered XML schema or are non-XML schema-based documents.

Creating XML Schema-Based Documents

To create XML schema-based documents, Java DOM API for XMLType uses an extension to specify which XML schema URL to use. For XML schema-based documents, it also verifies that the DOM being created conforms to the specified XML schema, that is, that the appropriate children are being inserted under the appropriate documents.


In this release, Java DOM API for XMLType does not perform type and constraint checks.

Once the DOM object has been created, it can be saved to Oracle XML DB Repository using the Oracle XML DB Resource API for Java. The XML document is stored in the appropriate format:

Example 9-8 Java DOM API for XMLType: Creating a DOM Object and Storing It in the Format Specified by the XML Schema

The following example shows how you can use Java DOM API for XMLType to create a DOM object and store it in the format specified by the XML schema. Note that the validation against the XML schema is not shown here.

import oracle.xdb.XMLType;
OraclePreparedStatement stmt = 
    (OraclePreparedStatement) conn.prepareStatement(
        "update po_xml_tab set poDoc = ? ");

// the second argument is a string
String poString = "<PO><PONO>200</PONO><PNAME>PO_2</PNAME></PO>";
XMLType poXML = XMLType.createXML(conn, poString);
Document poDOM = (Document)poXML.getDOM();

Element rootElem = poDOM.createElement("PO");
poDOM.insertBefore(poDOM, rootElem, null);

// now bind the string..


An XMLType instance is represented in Java by oracle.xdb.XMLType. When an instance of XMLType is fetched using JDBC, it is automatically manifested as an object of the provided XMLType class. Similarly, objects of this class can be bound as values to Data Manipulation Language (DML) statements where an XMLType is expected. The same behavior is supported in SQLJ clients.

Java DOM API for XMLType Classes

Oracle XML DB supports the W3C DOM Level 2 Recommendation. In addition to the W3C Recommendation, Oracle XML DB DOM API also provides Oracle-specific extensions, mainly to facilitate your application interfacing with Oracle XDK for Java. A list of the Oracle extensions is found at:

XMLDocument() is a class that represents the DOM for the instantiated XML document. You can retrieve the XMLType from the XML document using the function getXMLType() on XMLDocument() class.

Table 9-1 lists the Java DOM API for XMLType classes and the W3C DOM interfaces they implement.

Table 9-1 Java DOM API for XMLType: Classes  
Java DOM API for XMLType Class W3C DOM Interface Recommendation Class





























Non-Supported Java Methods

The following are methods documented in Release 2 ( but not currently supported:

Java DOM API for XMLType: Calling Sequence

The following Java DOM API for XMLType calling sequence description assumes that your XML data is pre-registered with an XML schema and that it is stored in an XMLType datatype column. To use the Java DOM API for XMLType, follow these steps:

  1. Retrieve the XML data from the XMLType table or XMLType column in the table. When you fetch XML data, Oracle XML DB creates a DOMDocument instance of XMLType, parsing the document into a DOM tree. You can then manipulate elements in the DOM tree using Java DOM API for XMLType.
  2. Use the Java DOM API for XMLType to perform operations and manipulations on elements of the DOM tree.
  3. The Java DOM API for XMLType sends the changed XML data back to Oracle XML DB.

Figure 9-1 illustrates the Java DOM API for XMLType calling sequence.

Figure 9-1 Java DOM API for XMLType: Calling Sequence

Text description of adxdb024.gif follows
Text description of the illustration adxdb024.gif