Oracle 如何使用xmltable处理xml
我有一个包含如下关系数据的XMLOracle 如何使用xmltable处理xml,oracle,plsql,xml-parsing,Oracle,Plsql,Xml Parsing,我有一个包含如下关系数据的XML <object name="object1"> <attribute name="attribute1"/> <attribute name="attribute2"> <value>value 1</value> <value>value 2</value> </attribute> </object> &
<object name="object1">
<attribute name="attribute1"/>
<attribute name="attribute2">
<value>value 1</value>
<value>value 2</value>
</attribute>
</object>
<object name="object2">
<attribute name="attribute1"/>
<attribute name="attribute2">
<value>value 1</value>
<value>value 2</value>
</attribute>
</object>
这是正确的方法吗?或者你将如何做到这一点?很抱歉,这太不完整了,但我希望它能指引正确的方向 您的解决方案执行大量XML解析。它可能会起作用,但如果有大量数据需要处理,我想可能需要一段时间。为XML编制索引可能会有所帮助,并且可能使您的方法可以接受
更好的解决方案可能是使用DBMS_XMLSAVE或DBMS_XMLSTORE包。很抱歉,这太不完整了,但我希望它朝着正确的方向发展 您的解决方案执行大量XML解析。它可能会起作用,但如果有大量数据需要处理,我想可能需要一段时间。为XML编制索引可能会有所帮助,并且可能使您的方法可以接受 更好的解决方案可能是使用DBMS_XMLSAVE或DBMS_XMLSTORE包
declare
-- This cursor retreives a record for each object with it's attribute values and
-- the underlying attributes as XMLtype
cursor c_object(p_xml xmltype) is
select obj.*
from xmltable('$d//object' passing p_xml as "d"
columns ... object columns ...
,attributes xmltype path 'attribute') as obj;
-- This cursor retreives a record for each attribute with it's attribute values and
-- the underlying values as XMLtype
cursor c_attributes(p_xml xmltype) is
select att.*
from xmltable('$d//object' passing p_xml as "d"
columns ... attribute columns ...
,values xmltype path 'value') as att;
-- This cursor retreives a record for each attribute with it's attribute values and
-- the underlying values as XMLtype
cursor c_values(p_xml xmltype) is
select val.*
from xmltable('$d//object' passing p_xml as "d"
,columns value varcahr2(100) path 'value') as val;
begin
-- p_xml contains the XML document
for r_obj in c_obj(p_xml) loop
-- insert an object record
insert into object...
for r_att in c_att(r_obj.attributes) loop
-- insert into attribute
insert into attribute ...
for r_val in c_val(r_att.values) loop
-- insert into values
end loop;
end loop;
end loop;
end;