Reputation: 625
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>[email protected]</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.
Upvotes: 0
Views: 1318
Reputation: 21115
You should preferably use XMLTABLE as the extractValue was deprecated
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>[email protected]</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 [email protected] 1 1901
69 9/30/2016 abc [email protected] 2 1902
69 9/30/2016 abc [email protected] 3 1903
Upvotes: 1
Reputation: 1138
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>[email protected]</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.
Upvotes: 1