将xml导入oracle数据库表

时间:2016-12-02 09:55:14

标签: sql xml oracle

我有这个示例xml我的要求是解析它并从它的各个节点获取值并将它们插入到oracle表中。

<?xml version="1.0" encoding="UTF-8"?>
<Reporting>
    <Selection>69</Selection>
    <MonthEndDate>9/30/2016</MonthEndDate>
    <Email>
        <Name>abc</Name>
        <Address>abc@gmail.com</Address>
    </Email>
    <Request>
        <Port_id_list>
            <Port_id>1901</Port_id>
            <Port_id>1902</Port_id>
            <Port_id>1903</Port_id>
        </Port_id_list>
    </Request>
</Reporting>

我们怎么做? 请帮忙。

2 个答案:

答案 0 :(得分:1)

已编辑

例如:

WITH my_data AS
(SELECT xmltype('<?xml version="1.0" encoding="UTF-8"?>
<Reporting>
    <Selection>69</Selection>
    <MonthEndDate>9/30/2016</MonthEndDate>
    <Email>
        <Name>abc</Name>
        <Address>abc@gmail.com</Address>
    </Email>
    <Request>
        <Port_id_list>
            <Port_id>1901</Port_id>
            <Port_id>1902</Port_id>
            <Port_id>1903</Port_id>
        </Port_id_list>
    </Request>
</Reporting>') my_xml
  FROM dual)
SELECT extractValue(md.my_xml, 'Reporting/Selection/text()') selection_id,
       extractValue(value(port_ids), 'Port_id/text()') port_id
  FROM my_data md,
       TABLE(XMLSequence (md.my_xml.extract ('Reporting/Request/Port_id_list/Port_id'))) port_ids;

如果您的XML具有子节点,则使用TABLE和XMLSequence创建一对多。

答案 1 :(得分:1)

您最好使用XMLTABLE,因为extractValue为deprecate d

这里有一个示例选择具有(非规范化)parten属性的端口。 我还添加了posr序列以保留端口的顺序。

WITH t AS
(SELECT xmltype('<?xml version="1.0" encoding="UTF-8"?>
<Reporting>
    <Selection>69</Selection>
    <MonthEndDate>9/30/2016</MonthEndDate>
    <Email>
        <Name>abc</Name>
        <Address>abc@gmail.com</Address>
    </Email>
    <Request>
        <Port_id_list>
            <Port_id>1901</Port_id>
            <Port_id>1902</Port_id>
            <Port_id>1903</Port_id>
        </Port_id_list>
    </Request>
</Reporting>')  xml
  FROM dual)
select  
x.Selection,x.MonthEndDate,x.emailName, x.emailAddress,
o.port_seq, o.port_id 
from t,   
        XMLTable(
          'for $i in /Reporting     
           return $i'
          passing t.xml 
          columns
                 Selection varchar2(30) path 'Selection',
                 MonthEndDate varchar2(30) path 'MonthEndDate',
                 emailName varchar2(30) path 'Email/Name',
                 emailAddress varchar2(30) path 'Email/Address',
                 Port_id_list XMLType path '//Port_id'
                  ) x,
         XMLTable(
          './Port_id'
          passing  (x.Port_id_list)
          columns
                port_seq for ordinality,
                port_id varchar2(30) path '/Port_id' 
                  ) o                   
;

SELECTION                      MONTHENDDATE                   EMAILNAME                      EMAILADDRESS                     PORT_SEQ PORT_ID                      
------------------------------ ------------------------------ ------------------------------ ------------------------------ ---------- ------------------------------
69                             9/30/2016                      abc                            abc@gmail.com                           1 1901                           
69                             9/30/2016                      abc                            abc@gmail.com                           2 1902                           
69                             9/30/2016                      abc                            abc@gmail.com                           3 1903