首页 > 数据库 > Oracle > xmltype数据类演练及读取效率测算
xmltype 预定义的对象类型
--1.演练部分
--用于保存和操作xml数据
--创建一个可以装xml的表
create table fallsxml(
fall_id number,
fall_xml xmltype
);
--使用静态方法CreateXML 插入第1条xml记录
INSERT INTO fallsxml
VALUES
(1,
xmltype.CreateXML('
[url]http://michiganwaterfalls.com/munising_falls/munising_falls.html[/url]
--插入第2条xml记录
INSERT INTO fallsxml
VALUES
(2,
XMLType.CreateXML('
[url]http://michiganwaterfalls.com/autrain_falls/autrain_falls.html[/url]
--插入第3条xml记录
INSERT INTO fallsxml
VALUES
(3,
XMLType.CreateXML('
--查看插入的记录,使用existsNode()方法检索节点
select t.*,rowid from fallsxml t;
select fall_id from fallsxml f where f.fall_xml.existsNode('/fall/url')>0;--存在fall/url
select fall_id from fallsxml where existsNode(fall_xml,'/fall/url')>0;--fall列中存在fall下面有url的
--抽取xml的文本并输出(获得指定字符,采用静态方法extract )
--在plqldev中运行报错(去掉2个demo_block即可),在sqlplus中正确
<
declare
v_xml xmltype;
url varchar2(500);
begin
--查询xml记录into给变量v_xml
select t.fall_xml into demo_block.v_xml from fallsxml t where t.fall_id = 1;
--从取到的xml记录中,抽取extract 节点值
url := v_xml.extract('/fall/url/text()').getStringVal();
dbms_output.put_line(url);
end;
/
--输出 [url]http://michiganwaterfalls.com/munising_falls/munising_falls.html[/url]
注:v_xml.extract('/fall/url/text( )').getStringVal 调用静态方法extract提取相应数据,也就是提供根目录下的url子目录下的文本,text()指url的体部文本。geteStringVal是文本的值。
select Upper(Xmltype.getStringVal(xmltype.extract(fall_xml,'/fall/url/text()'))) from fallsxml
where existsNode(fall_xml,'/fall/url')>0;
上面的是将查询出来的html地址变为大写
2.效率测算部分
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29267792/viewspace-1702052/ ,如需转载,请注明出处,否则将追究法律责任。