最近一个项目中用到了oracle的xmltype数据类型,发现这个网站比较详细,记录一下,比较简单,例子就不上传了:
http://stanford.edu/dept/itss/docs/oracle/10g/appdev.101/b10790/xdb11jav.htm#i1030427
12 Java API for XMLType
This chapter describes how to use XMLType
in Java, including fetching XMLType
data through JDBC.
This chapter contains these topics:
Introducing Java DOM API for XMLType
Oracle XML DB supports the Java Document Object Model (DOM) Application Program Interface (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
. DOM is an in-memory tree-based object representation of XML documents that enables programmatic access to their elements and attributes. The DOM object and interface are part of a W3C recommendation. DOM views the parsed document as a tree of objects.
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
because it can handle any valid XML document.
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 10, " 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.
Java DOM API for XMLType
can be used to construct an XMLType
instance from data encoded in different character sets. It also provides a new getBlobVal()
method to retrieve the XML contents in the requested character set.
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.
Accessing XML Documents Stored in Oracle 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 to Access XMLType Data
This is a SQL-based approach for Java applications for accessing any data in Oracle Database, including XML documents in Oracle XML DB. Use the oracle.xdb.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 12-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; while(orset.next()) { // get the XMLType XMLType poxml = XMLType.createXML(orset.getOPAQUE(1)); // get the XMLDocument as a string... Document podoc = (Document)poxml.getDOM(); }
Example 12-2 XMLType Java: Selecting XMLType Data
You can select the XMLType
data in JDBC in one of two ways:
-
Use the
getClobVal()
,getStringVal()
orgetBlobVal(csid)
in SQL and get the result as anoracle.sql.CLOB, java.lang.String
ororacle.sql.BLOB
in Java. The following Java code snippet shows how to do this:DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); Connection conn = DriverManager.getConnection("jdbc:oracle:oci8:@", "scott", "tiger"); OraclePreparedStatement stmt = (OraclePreparedStatement) conn.prepareStatement( "select e.poDoc.getClobVal() poDoc, "+ "e.poDoc.getStringVal() poString "+ " from po_xml_tab e"); ResultSet rset = stmt.executeQuery(); Oracle