关于Oracle的XMLType类型的数据库操作

来源:互联网 发布:韶关市始兴县网络问政 编辑:程序博客网 时间:2024/06/03 11:47

oracle从9i开始支持一种新的数据类型- xmltype,用于存储和管理xml数据,并提供了很多的functions,用来直接读取xml文档和管理节点。下面介绍xmltype的一些基本使用。

1.建立含有xmltype数据类型的表

create table test (

id number,

xmldoc sys.xmltype

);

声明xmltype型字段用:sys.xmltype

 

2.向带有xmltype类型的表插入带有数据

insert into test (id,xmldoc) values (abc.nextval , sys.xmlType.createXML('' <name><a id="1" value="some values">test</a></name>'') );

插入用 sys.xmlType.createXML(''xml doc'')

 

3.直接查询xmltype字段里面的内容

得到id=1的value变脸的值

select t.xmldoc.extract(''//name/a[@id=1]/@value'').getStringVal() as ennames, id from test t

 

得到a节点的值

select id,t.xmldoc.extract(''//name/a/text()'').getStringVal() as truename from test t

 

4.更新xmltype里面的数据

update abc set xmldoc=updateXML(xmldoc,''//name/a[@id=1]/@value'',''some new value'') where ......

(注意:如果里面没有<a id="1">这个节点,将不能update)

 

5.添加超过4k字节的xml文档到xmltype型字段

可以通过使用临时表的办法实现:

先建立一个临时的表temp,其中的一个字段是clob类型;

再将要写入xmltype字段的xml doc写入这个临时的clob型的字段中;

最后insert into test (id,xmldoc) values (test_q.nextval , sys.xmlType.createXML((select content from temp where id=......)));

 

6. 更新表中xmltype字段的数据

update test t set t.xmldoc = sys.XMLType.createXML(select content from temp where id=……);

0 0