user1750160
user1750160

Reputation: 11

xml to oracle database

I have to import an xml file into an oracle data base, but the file have this structure :

tasks:

  1. Parse an given XML file and import it in an Oracle database
  2. Create database based on the XML structure

xml file:

  <root>
    <customers>
     <customer id=1>e-mag</customer>
     <customer id=2>Eurofigher</customer>
    </customers>
    <customer_invoices>
     <invoice id=1>
      <customer_id>1</customer_id>
      <items>
       <item id=1 unit_value=10 tva=21 units=5>Laptop</item>
       <item id=2 unit_value=20 tva=21 units=3>Monitors</item>
      </items>
     </invoice> 
    </customer_invoices>
  </root>

What is the best way to do that? Please help me.

Upvotes: 0

Views: 206

Answers (1)

Rob van Laarhoven
Rob van Laarhoven

Reputation: 8905

If you want to import them in an relational structure. First load it into a XMLTYPE column

DROP TABLE XMLTEST;

CREATE TABLE XMLTEST
(   XML_COL XMLTYPE);

DECLARE
  poXML CLOB; 
BEGIN   
  -- Store the Purchase Order XML in the CLOB variable
  poXML := '<?xml version="1.0"?>
<rooms>
    <room room_id="1">
        <alt_id>88</alt_id>
        <display_naam>01 West 430</display_naam>
        <alt_db>eXpress_BK</alt_db>
    </room>
    <room room_id="2">
        <alt_id>170</alt_id>
        <display_naam>02 Midden 010</display_naam>
        <alt_db>eXpress_BK</alt_db>
    </room>
    <room room_id="3">
        <alt_id>173</alt_id>
        <display_naam>02 Midden 110</display_naam>
        <alt_db>eXpress_BK</alt_db>
    </room>
    <room room_id="4">
        <sil_id>F491B0A119DABE76B2F6B2C0A3E902F6</sil_id>
        <alt_id>183</alt_id>
        <display_naam>02 Oost 010</display_naam>
        <alt_db>eXpress_BK</alt_db>
    </room>
    <room room_id="5">
        <alt_id>172</alt_id>
        <display_naam>02 Oost 300</display_naam>
        <alt_db>eXpress_BK</alt_db>
    </room>
  .
  .
  .
    <room room_id="126">
        <sil_id>F491B0A119DABE76B2F6B2C0A3E901E3</sil_id>
        <alt_id>129</alt_id>
        <display_naam>HB.02.140</display_naam>
        <alt_db>eXpress_EWI</alt_db>
    </room>
</rooms>';

  INSERT INTO xmltest (xml_col) VALUES (XMLTYPE(poXML));

END;

Then create a table based ont the XML structure

drop table rooms;
create table rooms as 
select xt.room_id
,      xt.alt_id
,      xt.sil_id
,      xt.alt_db
,      xt.display_naam
from xmltest xts
,    XMLTable('rooms/room' PASSING xts.xml_col 
                   columns room_id INTEGER PATH '@room_id'
                             ,alt_id INTEGER PATH 'alt_id'
                             ,sil_id VARCHAR2(100) PATH 'sil_id'
                             ,display_naam VARCHAR2(100)PATH 'display_naam'
                             ,alt_db VARCHAR2(100)PATH 'alt_db') xt;

I think you will have to create three create table statements. One for customers and one for customer_invoices and one form cursomer_invoice_items as they clearly don't fit in one relational table.

Upvotes: 2

Related Questions