简体   繁体   English

将xml导入oracle数据库表

[英]import xml to oracle database table

I have this sample xml my requirement is to parse it and fetch values from it's various nodes and insert them into one of oracle table. 我有这个示例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>

How can we do this? 我们应该怎么做? Please help. 请帮忙。

EDITED 已编辑

For example: 例如:

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;

If your XML has node with children create one-to-many with TABLE and XMLSequence. 如果您的XML具有带有子节点的节点,则使用TABLE和XMLSequence创建一对多。

You should preferably use XMLTABLE as the extractValue was deprecate d 您最好使用XMLTABLE,因为extractValue已弃用 d

Here an example selecting teh ports with the (denormalized) parten attributes. 在此示例中,选择具有(非规范化)parten属性的端口。 I added also posr sequence to preserve order of the ports. 我还添加了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   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM