简体   繁体   中英

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 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.

You should preferably use XMLTABLE as the extractValue was deprecate d

Here an example selecting teh ports with the (denormalized) parten attributes. I added also posr sequence to preserve order of the ports.

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   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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