-1

I´m trying to load an existent XML file into an existent table in oracle. My XML file as the structure:

enter image description here

<Invoice>
  <InvoiceNo>FS TML0001/003123501</InvoiceNo>
  <ATCUD>JFRWKFDT-003123501</ATCUD>
  <DocumentStatus>
    <InvoiceStatus>N</InvoiceStatus>
    <InvoiceStatusDate>2025-04-01T14:32:02</InvoiceStatusDate>
    <SourceID>98778400</SourceID>
    <SourceBilling>P</SourceBilling>
  </DocumentStatus>
  <Hash>LzIHA5diGz++Py1HCXPUph9hDT2aKNUDGa3bvxPlLwPpGsSsItEe2gEBLEfG6NjpLIep/d1VCg90lGAi67YhlwCpIObCe6T3GzDbXR8dSdlv0xuSFpZ9fsWnfmTgXM8uO9LRAW1Ki40GKMIdqppYt67wvPSrqtjPoIn2afvE7bI=</Hash>
  <HashControl>1</HashControl>
  <Period>04</Period>
  <InvoiceDate>2025-04-01</InvoiceDate>
  <InvoiceType>FS</InvoiceType>
  <SpecialRegimes>
    <SelfBillingIndicator>0</SelfBillingIndicator>
    <CashVATSchemeIndicator>0</CashVATSchemeIndicator>
    <ThirdPartiesBillingIndicator>1</ThirdPartiesBillingIndicator>
  </SpecialRegimes>
  <SourceID>98778400</SourceID>
  <EACCode>49310</EACCode>
  <SystemEntryDate>2025-04-01T14:31:30</SystemEntryDate>
  <CustomerID>307497321</CustomerID>
  <Line>
    <LineNumber>1</LineNumber>
    <ProductCode>CTG22_ProductGroup_5</ProductCode>
    <ProductDescription>Navegante Lisboa</ProductDescription>
    <Quantity>1</Quantity>
    <UnitOfMeasure>UN</UnitOfMeasure>
    <UnitPrice>28.3000</UnitPrice>
    <TaxPointDate>2025-04-01</TaxPointDate>
    <Description>3310713506</Description>
    <CreditAmount>28.3000</CreditAmount>
    <Tax>
      <TaxType>IVA</TaxType>
      <TaxCountryRegion>PT</TaxCountryRegion>
      <TaxCode>RED</TaxCode>
      <TaxPercentage>6.00</TaxPercentage>
    </Tax>
  </Line>
  <DocumentTotals>
    <TaxPayable>1.7000</TaxPayable>
    <NetTotal>28.3000</NetTotal>
    <GrossTotal>30.00</GrossTotal>
  </DocumentTotals>
</Invoice>


<Invoice>
  <InvoiceNo>FS TML0001/003123502</InvoiceNo>
  <ATCUD>JFRWKFDT-003123502</ATCUD>
  <DocumentStatus>
    <InvoiceStatus>N</InvoiceStatus>
    <InvoiceStatusDate>2025-04-01T14:32:04</InvoiceStatusDate>
    <SourceID>98774198</SourceID>
    <SourceBilling>P</SourceBilling>
  </DocumentStatus>
  <Hash>ZgSvYKf+Wtc8lSWTkHgpbnTy3ksT60gKT4umVcIfeQiW0G1MBySSGQ3foEzWFoPSSgA3ua49eTx/f9kUjM0+o4h0F0dEO7kF/xg938GT8ps3piqu1YX4V+nlcqWmysNGnrQGfSAPuIDeBO3Iq3dvGXHhoixl/8qx14kVtwzDai8=</Hash>
  <HashControl>1</HashControl>
  <Period>04</Period>
  <InvoiceDate>2025-04-01</InvoiceDate>
  <InvoiceType>FS</InvoiceType>
  <SpecialRegimes>
    <SelfBillingIndicator>0</SelfBillingIndicator>
    <CashVATSchemeIndicator>0</CashVATSchemeIndicator>
    <ThirdPartiesBillingIndicator>1</ThirdPartiesBillingIndicator>
  </SpecialRegimes>
  <SourceID>98774198</SourceID>
  <EACCode>49310</EACCode>
  <SystemEntryDate>2025-04-01T14:31:34</SystemEntryDate>
  <CustomerID>999999990</CustomerID>
  <Line>
    <LineNumber>1</LineNumber>
    <ProductCode>CTG22_ProductGroup_10</ProductCode>
    <ProductDescription>PAC navegante +65</ProductDescription>
    <Quantity>1</Quantity>
    <UnitOfMeasure>UN</UnitOfMeasure>
    <UnitPrice>0.00</UnitPrice>
    <TaxPointDate>2025-04-01</TaxPointDate>
    <Description>3310669876</Description>
    <CreditAmount>0.00</CreditAmount>
    <Tax>
      <TaxType>IVA</TaxType>
      <TaxCountryRegion>PT</TaxCountryRegion>
      <TaxCode>RED</TaxCode>
      <TaxPercentage>6.00</TaxPercentage>
    </Tax>
    <SettlementAmount>37.7359</SettlementAmount>
  </Line>
  <DocumentTotals>
    <TaxPayable>0.00</TaxPayable>
    <NetTotal>0.00</NetTotal>
    <GrossTotal>0.00</GrossTotal>
  </DocumentTotals>
</Invoice>

I just nedd a procedure that:

1- Read a file in my local computer and the stored procedure receive a parameter and this parameter is the file in my local computer (C:\tmp p.e.)

2- read the file and insert in table xml_final_table the content of each tag in header. (LineNumber, ProductCode.....) 3- I do not want to load the all file to a table and after to a final table. I want to load all the information to a final table.

I already have the code:

DECLARE
  x XMLType := XMLType(
    '<?xml version="1.0" ?> 
<person>
   <row>
       <name>Tom</name>
       <Address>
           <State>California</State>
           <City>Los angeles</City>
       </Address>
   </row>
   <row>
       <name>Jim</name>
       <Address>
           <State>California</State>
           <City>Los angeles</City>
       </Address>
   </row>
</person>');
BEGIN
  FOR r IN (
    SELECT ExtractValue(Value(p),'/row/name/text()') as name
          ,ExtractValue(Value(p),'/row/Address/State/text()') as state
          ,ExtractValue(Value(p),'/row/Address/City/text()') as city
    FROM   TABLE(XMLSequence(Extract(x,'/person/row'))) p
    ) LOOP
    -- do whatever you want with r.name, r.state, r.city
  END LOOP;
END; 

But is not solving my problem and i´m lost who can I do that?

Any help please?

4
  • "I already have the code..." Your code bears very little relation to the problem you are trying to solve. What have YOU tried to: (a) read XML from a file; (b) parse the XML to extract the data; and (c) INSERT the parsed data into a table? At the moment it looks like you are asking us to write the entirety of your code for you rather than debugging an attempted solution. Commented May 13 at 7:29
  • Your XML is invalid as it has multiple <Invoice> elements at the root. An XML document must have a single element at the root. Commented May 13 at 7:31
  • @MT0 I have not enough technical skills to build the procedure. I´m just trying to surprise my boss by show him that his problema may be resolved. But I do understand your point and if you cannot help me I understand. Commented May 13 at 9:16
  • @MT0 the XMl as a single element at the root. I just past, what its suposed to be loaded Commented May 13 at 9:17

1 Answer 1

1

Given your sample code, you can use XMLTABLE and an INSERT INTO ... SELECT ... statement to parse the XML and insert it into the table:

DECLARE
  x XMLType := XMLType(
    '<?xml version="1.0" ?> 
<person>
   <row>
       <name>Tom</name>
       <Address>
           <State>California</State>
           <City>Los angeles</City>
       </Address>
   </row>
   <row>
       <name>Jim</name>
       <Address>
           <State>California</State>
           <City>Los angeles</City>
       </Address>
   </row>
</person>');
BEGIN
  INSERT INTO table_name (name, state, city)
  SELECT name, state, city
  FROM   XMLTABLE(
           '/person/row'
           PASSING x
           COLUMNS
             name  VARCHAR2(50) PATH './name',
             state VARCHAR2(50) PATH './Address/State',
             city  VARCHAR2(50) PATH './Address/City'
         );
END;
/

If you want to put the code into a procedure the replace the DECLARE clause with the CREATE PROCEDURE signature.

After running the PL/SQL block, the table will contain:

NAME STATE CITY
Tom California Los angeles
Jim California Los angeles

If you have XML in a different structure then adapt the paths to your data; this is left as an exercise to the reader.

fiddle

Sign up to request clarification or add additional context in comments.

2 Comments

can you please help me how to load data from an XML in a local path in my computer please? Instead of have all XML file in the procedure body
@NelsonSoares There are many resources available online - see 1 2 3 If you are having problems with a particular bit of code then ask a new question and provide a minimal reproducible example with YOUR attempt at a solution. Don't ask people to do all your work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.