Oracle xml實踐

官方參考文檔:XML DB Developer's Guide(11.2)

1、帶有XMLType字段的表操作

-- Create table
create table T_XML_TEST
(
  id        NUMBER,
  xml_value XMLTYPE
);
--使用xmltype數據類型的靜態方法createxml插入xml數據
INSERT INTO T_XML_TEST(id, xml_value) VALUES (1, XMLType.CreateXML(
       '<?xml version="1.0"?>
         <Envelope>
           <Body>
              <sayHelloResponse>
                 <return>94065中國 say: hello [axis2]</return>
              </sayHelloResponse>
         </Body>
      </Envelope>''));
--查詢帶xmltype類型字段的表的方法
select t.id, t.xml_value.getclobval() xml_value from T_XML_TEST t;


2、XMLTABLE函數使用,檢索xml節點的值

(1)xml不帶命名空間,如下xml:

<?xml version="1.0"?>
  <Envelope>
     <Body>
        <sayHelloResponse>
           <return>94065中國 say: hello [axis2]</return>
        </sayHelloResponse>
     </Body>
  </Envelope>

示例:

SELECT t."return", t."return2"
  FROM 
       XMLTABLE(XMLNAMESPACES('http://www.w3.org/2003/05/soap-envelope' as "soap",
                 'http://ws.apache.org/axis2' as "tns"
                 )
                ,'/Envelope' PASSING Xmltype.createxml(
                '<?xml version="1.0"?>
                   <Envelope>
                     <Body>
                       <sayHelloResponse>
                         <return>94065中國 say: hello [axis2]</return>
                       </sayHelloResponse>
                     </Body>
                   </Envelope>'
                )
                COLUMNS 
                "return" varchar2(100) PATH '/Envelope/Body/sayHelloResponse/return'
                ,"return2" varchar2(100) PATH '/Envelope/Body/sayHelloResponse/return'
                ) t;

輸出結果:

(2)xml帶有命名空間,如下xml:

<?xml version="1.0" encoding="UTF-8"?>
  <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://ws.apache.org/axis2">
    <soap:Body>
      <tns:delete>
        <tns:record_id>7352</tns:record_id>
      </tns:delete>
    </soap:Body>
  </soap:Envelope>

示例:

SELECT t."return", t."return2"
  FROM 
       XMLTABLE(XMLNAMESPACES('http://www.w3.org/2003/05/soap-envelope' as "soap",
                 'http://ws.apache.org/axis2' as "tns"
                 )
                ,'/soap:Envelope' PASSING Xmltype.createxml(
                '<?xml version="1.0" encoding="UTF-8"?>
                  <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://ws.apache.org/axis2">
                   <soap:Body>
                    <tns:delete>
                     <tns:record_id>7352</tns:record_id>
                    </tns:delete>
                   </soap:Body>
                  </soap:Envelope>'
                )
                COLUMNS 
                "return" varchar2(10) PATH '/soap:Envelope/soap:Body/tns:delete/tns:record_id'
                ,"return2" varchar2(10) PATH '/soap:Envelope/soap:Body/tns:delete/tns:record_id'
                ) t

輸出結果:

(3)附加where條件

SELECT t.id, t.xml_value.getclobval(), t2."return"
  FROM T_XML_TEST t,
       XMLTABLE(XMLNAMESPACES('http://www.w3.org/2003/05/soap-envelope' as "soap",
                 'http://ws.apache.org/axis2' as "tns"
                 )
                ,'/soap:Envelope' PASSING t.xml_value COLUMNS
                "return" varchar2(10) PATH '/soap:Envelope/soap:Body/tns:delete/tns:record_id') t2
  where t.id = 254;


 


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章