工做之餘,總結一下xml操做的一些方法和心得!node
tip: xmltype函數是將clob字段轉成xmltype類型的函數,若字段自己爲xmltype類型則不須要引用xmltype()函數數組
同名標籤用數組取值的方式獲取,但起始值從1開始函數
一.查詢(Query)spa
1. extract函數,查詢節點值,帶節點名rest
1 -- 獲取帶節點的值,例如:<tel>222</tel> 2 select extract(xmltype(e.prj_xml),'/data/project/persons/person[1]/tel').getStringVal() as title from project e where e.zh_title='白夜追逐繁星'; 3 -- 備註若是節點表達式查詢一個節點的父節點,則會將該父節點下的全部節點包含該父節點查出
Query Result:code
tip: extract函數中路徑引用text(),查詢的節點若重複則自動拼接xml
select extractvalue(xmltype('<a><b>1</b><b>2</b></a>'),'/a/b') from dual; -- 報錯,報只返回一個節點值,由於在a標籤下存在兩個同名標籤b select extract(xmltype('<a><b>1</b><b>2</b></a>'),'/a/b/text()') from dual; -- extract+text() 解決同名節點問題,若存在重複節點會自動拼接在一塊兒,但不使用任何拼接符號
2. extractvalue函數,查詢節點值,不帶節點名blog
-- 獲取不帶節點的值,例如:222 1 select extractvalue(xmltype(e.prj_xml),'/data/project/persons/person[1]/tel') as result from project e where e.zh_title='白夜追逐繁星';
Query Result:ip
Tip: 節點不存在時,查詢結果均爲空element
3. existsnode函數,判斷節點是否存在,表示存在,0表示不存在
1 select existsnode(xmltype(e.prj_xml),'/data/project/persons/person[1]/tel') as result from project e where e.zh_title='白夜追逐繁星';
Query Result:
4. sys_dburigen,將指定列經過函數生成一個該列的URI值,例如:/PUBLIC/PROJECT/ROW[ZH_TITLE='鄒成咁180117']/ZH_TITLE
1 select sys_dburigen(e.zh_title) as result from project e where e.zh_title='白夜追逐繁星';
Query Result:
5. sys_xmlAgg,合併查詢,將多個xml合併,相似於set集合
-- sys_xmlGen 將xml轉成xmltype實例,方便xml合併,sys_xmlAgg用於xml合併
1 select sys_xmlAgg(sys_xmlgen(e.prj_xml)) as result from project e where e.zh_title='白夜追逐繁星' or e.zh_title='白夜追逐繁星2' ;
Query Result:
6. xmlforest,將指定列以xml格式查詢出來,可指定生成的xml節點名稱
1 select xmlforest(e.zh_title as zhTitle,e.prj_no as prjNo,e.psn_code as psnCode).getStringVal() as xml from project e where e.zh_title='白夜追逐繁星';
Query Result:
7. xmlelement,爲查詢出來的xml添加掛載的父節點,並將xml字符串格式化成xml ,與xmlforest函數配套使用
1 select xmlelement(data,xmlforest(e.zh_title,e.prj_no,e.psn_code)).getStringVal() as xml from project e where e.zh_title='白夜追逐繁星';
Query Result:
延伸:爲data節點添加屬性,使用xmlattributes函數
1 select xmlelement(data,xmlattributes(e.prj_code as code),xmlforest(e.zh_title,e.prj_no,e.psn_code)).getStringVal() as xml from project e where e.zh_title='白夜追逐繁星';
Query Result:
延伸: XMLCOLATTVAL效果等同於xmlforest函數,但默認會爲每一個標籤添加一個屬性name,屬性值爲列明,若未指定列別名,默認該列列明
1 select XMLCOLATTVAL(e.zh_title as zhTitle,e.prj_no as prjNo,e.psn_code as psnCode).getStringVal() as xml from project e where e.zh_title='白夜追逐繁星'
Query Result:
8. xmlConcat,xmlType實例之間聯結
1 select xmlelement(data,xmlConcat(xmltype('<a>1</a>'),xmltype('<b>1</b>'))).getStringVal() as result from dual;
Query Result:
9. xmlsequence將一個xml以標籤爲單位,轉換成數組,也就是一行行記錄
1 select e.getStringVal() as result 2 from table(xmlsequence(extract(xmltype('<a><b>233</b><c>666</c><d>88</d></a>'),'/a/*'))) e;
Query Result:
二.添加(Insert)
-- 添加xml節點,insertchildxml添加xml節點,參數3默認指定插在該節點後,若該節點不存在,則追加到子節點集合的末尾
-- 添加xml節點,insertchildxmlbefore,和insertchildxmlafter添加xml節點,參數3指定插在該節點前或者後,若該節點不存在,則追加到子節點集合的末尾
1 update project e set e.prj_xml=insertchildxml(xmltype(e.prj_xml),'/data/project/persons/person[1]','tel',xmltype('<tel>222</tel>')).getClobVal() where e.zh_title='白夜追逐繁星'; 2 update project e set e.prj_xml=insertchildxmlbefore(xmltype(e.prj_xml),'/data/project/persons/person[1]','psn_code',xmltype('<tel>111</tel>')).getClobVal() where e.zh_title='白夜追逐繁星';
三.修改(Update)
-- updatexml用於更新節點值
1 -- updatexml用於更新節點值,參數1:須要修改節點的xml字段;參數2:節點路徑;參數3:值 2 update project e set e.prj_xml=updatexml(xmltype(e.prj_xml),'/data/project/persons/person[1]/tel','<tel>111</tel>').getClobVal() where e.zh_title='白夜追逐繁星'; 3 update project e set e.prj_xml=updatexml(xmltype(e.prj_xml),'/data/project/persons/person[1]/tel/text()','222').getClobVal() where e.zh_title='白夜追逐繁星';
tip: getClobVal()是將xmltype類型轉成clob類型方法
四.刪除(Delete)
1 -- 刪除xml節點,參數1:須要刪除節點的xml字段;參數2:節點路徑; 2 update project e set e.prj_xml=deletexml(xmltype(e.prj_xml),'/data/project/persons/person[1]/tel').getClobVal() where e.zh_title='白夜追逐繁星';