首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用xmltable处理xml

如何使用xmltable处理xml
EN

Stack Overflow用户
提问于 2011-12-05 19:39:32
回答 1查看 2.3K关注 0票数 2

我有一个包含如下关系数据的XML

代码语言:javascript
复制
<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> 

我需要将其存储到以下关系模型中:

表对象

表属性FK到对象

表值FK到属性

使用XMLTable函数,我想出了以下解决方案:

代码语言:javascript
复制
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;    

这是正确的方法吗?或者你会怎么做呢?

EN

回答 1

Stack Overflow用户

发布于 2012-01-28 02:27:32

我很抱歉,这是如此可怕的不完整,但我希望它的方向是正确的。

您的解决方案执行了大量的XML解析。它可能会起作用,但如果有大量数据需要处理,我想这将需要一段时间。对XML进行索引可能会有所帮助,并可能使您的方法可接受。

更好的解决方案可能是使用DBMS_XMLSAVEDBMS_XMLSTORE包。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8384736

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档