初次接触oracle的xmltype

这篇博客介绍了如何在Java中使用Oracle的XMLType,包括通过JDBC访问XMLType数据的方法。文章涵盖了Java DOM API的功能,如创建XML文档、访问数据库中存储的XML文档,并提供了使用JDBC操作XMLType数据的示例。
摘要由CSDN通过智能技术生成

最近一个项目中用到了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() or getBlobVal(csid) in SQL and get the result as an oracle.sql.CLOB, java.lang.String or oracle.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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值