ORACLE XML操作初识

--创建
CREATE TABLE XMLTEST(
	RID VARCHAR2(50),
	XMLSTR XMLTYPE
);

--新增

INSERT INTO XMLTEST(RID, XMLSTR)
VALUES('1',
'<?xml version="1.0" encoding="UTF-8"?>
<info>
<name><![CDATA[娱乐新闻1]]></name>
<abstract><![CDATA[娱乐新闻1]]></abstract>
<keyword><![CDATA[娱乐新闻1]]></keyword>
<start_time><![CDATA[2009-11-07 10:39:00]]></start_time>
<stop_time><![CDATA[2009-11-10 10:39:00]]></stop_time>
<author><![CDATA[null]]></author>
<content><![CDATA[娱乐新闻1]]></content>
<pic_url><![CDATA[100000311006.jpg]]></pic_url>
<audio_url><![CDATA[100000311007.jpg]]></audio_url>
<video_url><![CDATA[100000311008.jpg]]></video_url>
<Owner>TOM</Owner>
<Owner>JACK</Owner>
<Owner>Grandco</Owner>
</info>');

INSERT INTO XMLTEST(rid,XMLSTR) 
VALUES(2,
    sys.xmltype.createxml(
    '<?xml version="1.0"?>
    <customer>
    <name>Joe Smith</name>
    <title>Mathematician</title>
    </customer>'));

INSERT INTO XMLTEST(rid,XMLSTR) 
VALUES(3,
    sys.xmltype.createxml(
    '<?xml version="1.0"?>
    <customer>
    <name>Kit</name>
    <title>Mathematician</title>
    </customer>'));

INSERT INTO XMLTEST(RID, XMLSTR)
VALUES('4',
'<?xml version="1.0" encoding="UTF-8"?>
<stus>
<stu>
	<name alias="jim">jack</name>
	<age>13</age>
	<skills part="wwe">
		<skill>f5</skill>
		<skill>ak47</skill>
	</skills>
</stu>
<stu>
	<name alias="">乔峰</name>
	<age>36</age>
	<skills></skills>
</stu>
</stus>');

--查询
--查询全部
SELECT RID,extract(XMLSTR,'/').getStringVal() XMLCONTENT
FROM XMLTEST;

--判断是否存在
SELECT extract(XMLSTR,'//info').getStringVal() 
FROM XMLTEST 
WHERE existsNode(XMLSTR, '//info/name') = 1;

--条件查询
SELECT extract(XMLSTR,'//customer').getStringVal() 
FROM XMLTEST 
WHERE EXTRACTVALUE(XMLSTR, '//customer/title') = 'Mathematician';

--更新
--追加添加
UPDATE XMLTEST SET XMLSTR =
APPENDCHILDXML(XMLSTR, 'customer/name', XMLType('<Owner>Grandco</Owner>'))
WHERE EXTRACTVALUE(warehouse_spec, '/customer/name') = 'Rented';

 --更新整个文档
UPDATE XMLTEST 
SET XMLSTR = XMLType('<PurchaseOrder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation= "http://localhost:8080/source/schemas/poSource/xsd/purchaseOrder.xsd"> <Reference>SBELL-2002100912333601PDT</Reference> <Actions> <Action> <User>SVOLLMAN</User> </Action> </Actions> <Reject/> <Requestor>Sarah J. Bell</Requestor> <User>SBELL</User> <CostCenter>S30</CostCenter> <ShippingInstructions> <name>Sarah J. Bell</name> <address>400 Oracle Parkway Redwood Shores CA 94065 USA</address> <telephone>650 506 7400</telephone> </ShippingInstructions> <SpecialInstructions>Air Mail</SpecialInstructions> <LineItems> <LineItem ItemNumber="1"> <Description>A Night to Remember</Description> <Part Id="715515009058" UnitPrice="39.95" Quantity="2"/> </LineItem> <LineItem ItemNumber="2"> <Description>The Unbearable Lightness Of Being</Description> <Part Id="37429140222" UnitPrice="29.95" Quantity="2"/> </LineItem> <LineItem ItemNumber="3"> <Description>Sisters</Description> <Part Id="715515011020" UnitPrice="29.95" Quantity="4"/> </LineItem> </LineItems> </PurchaseOrder>') 
WHERE RID='3';

--更新节点内容
UPDATE XMLTEST 
SET XMLSTR = updateXML(XMLSTR, '/PurchaseOrder/Actions/Action[1]/User/text()', 'SKING') 
WHERE RID='1';

--更新多个节点
update XMLTEST
set XMLSTR=updateXML(XMLSTR, 
'/stus/stu[1]/age/text()', '15',
'/stus/stu[2]/name/text()','萧峰',
'/stus/stu[2]/name/@alias','乔峰')
where rid='4';

--更新为空
update XMLTEST
set XMLSTR=null
where rid='3';

--数据节点移除为空
update XMLTEST
set XMLSTR=updateXML(XMLSTR,
	'/info/abstract/text()',null
)
WHERE rid='1'

--插入节点
UPDATE XMLTEST 
SET XMLSTR = insertChildXML(XMLSTR, 
'/stus/stu[2]/skills', 'skill', 
XMLType('<skill note="18z">降龙十八掌</skill>')) 
WHERE RID='4';
UPDATE XMLTEST 
SET XMLSTR = insertChildXML(XMLSTR, 
'/stus/stu[2]/skills', 'skill2', 
XMLType('<skill2 note="">轻功</skill2>')) 
WHERE RID='4';
UPDATE XMLTEST 
SET XMLSTR = insertChildXML(XMLSTR, 
'/stus/stu[2]/skills', 'skill3', 
XMLType('<skill3 note="">轻功</skill3>')) 
WHERE RID='4';

--插入节点before/after

--删除节点内容
UPDATE XMLTEST 
SET XMLSTR = deleteXML(XMLSTR, '/stus/stu[2]/skills/skill0') 
WHERE existsNode(XMLSTR, '/stus/stu[2]/skills/skill0')= 1 and rid='4';

--删除
DELETE XMLTEST
WHERE RID='111';


参考博文:http://blog.csdn.net/z81j06f03/article/details/6072753

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值