Solved

xml to Oracle Database

Posted on 2014-11-17
3
36 Views
Last Modified: 2016-06-15
My XML file: textfile.xml

<ATB>
<CLAIMDATA oid="1">
    <Claimno>101</Claimno>
    <patname>John,Miller</patname>
    <payors>
        <payordata>
            <inscode>MCR</inscode>
            <insbal>100.66</insbal>
            <inspriority>1</inspriority>
        </payordata>
        <payordata>
            <inscode>BCBS</inscode>
            <insbal>200.20</insbal>
            <inspriority>2</inspriority>
        </payordata>
    </payors>
</CLAIMDATA>
<CLAIMDATA oid="2">
    <claimno>102</claimno>
    <patname>Louis,Phillipe</patname>
    <payors>
        <payordata>
            <inscode>TUFTS</inscode>
            <insbal> 1010.50</insbal>
            <inspriority>1</inspriority>
        </payordata>
    </payors>
</CLAIMDATA>
</ATB>

I have written the following query which gives me two empty rows with out any data.

WITH g AS (SELECT xmltype(bfilename('SAMP_XML_DIR','textfile.xml'), nls_charset_id('WE8ISO8859P1')) xmlcol1 FROM dual)
SELECT
extractValue(value(x),'ATB/CLAIMDATA/Claimno') claimno        
,extractValue(value(x),'ATB/CLAIMDATA/patname') patname  
,extractValue(value(x),'ATB/CLAIMDATA/payors/payordata/inscode[1]') ins1code
,extractValue(value(x),'ATB/CLAIMDATA/payors/payordata/insbal[1]') ins1bal
,extractValue(value(x),'ATB/CLAIMDATA/payors/payordata/inspriority[1]') ins1priority
,extractValue(value(x),'ATB/CLAIMDATA/payors/payordata/inscode[2]') ins2code
,extractValue(value(x),'ATB/CLAIMDATA/payors/payordata/insbal[2]') ins2bal
,extractValue(value(x),'ATB/CLAIMDATA/payors/payordata/inspriority[2]') ins2priority
FROM g,TABLE(XMLSequence(extract(g.xmlcol1,'ATB/CLAIMDATA'))) x;

Please help me in resolving the issue and getting the data  through this query.

Thanks in advance,
Sunil.
0
Comment
Question by:sunil allada
3 Comments
 
LVL 76

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
ID: 40447728
extract and extractvalue is deprecated and shouldn't be used anymore.

use XMLTABLE instead.

See if the example below works for you.

Note:  I use CTE (WITH) to get the XML but the query should work fine using bfilename.

Also note I had to tweak the XML you posted.  You had 'Claimno' in one node and 'claimno' in another.

with mydata as (
select xmltype('
<ATB>
 <CLAIMDATA oid="1">
     <Claimno>101</Claimno>
     <patname>John,Miller</patname>
     <payors>
         <payordata>
             <inscode>MCR</inscode>
             <insbal>100.66</insbal>
             <inspriority>1</inspriority>
         </payordata>
         <payordata>
             <inscode>BCBS</inscode>
             <insbal>200.20</insbal>
             <inspriority>2</inspriority>
         </payordata>
     </payors>
 </CLAIMDATA>
 <CLAIMDATA oid="2">
     <Claimno>102</Claimno>
     <patname>Louis,Phillipe</patname>
     <payors>
         <payordata>
             <inscode>TUFTS</inscode>
             <insbal> 1010.50</insbal>
             <inspriority>1</inspriority>
         </payordata>
     </payors>
 </CLAIMDATA>
 </ATB>') my_xml
 from dual
)
select claimno, inscode, insbal, inspriority
from mydata,
	xmltable(
     '/ATB/CLAIMDATA'
     passing my_xml
       columns
            claimno varchar2(10) path '/CLAIMDATA/Claimno',
            payors xmltype path '/CLAIMDATA/payors'
    ) atb_xml,
    xmltable(
    '/payors/payordata'
    passing payors
    columns
            inscode varchar2(10) path '/payordata/inscode',
            insbal varchar2(10) path '/payordata/insbal',
            inspriority varchar2(10) path '/payordata/inspriority'
    )
/

Open in new window

0
 

Author Comment

by:sunil allada
ID: 40466768
Thank you so much.  Let me try that and will come back if I  get any error
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Oracle dataguard 5 30
SQL Server 2012 r2 - Varible Table 3 24
convert null in sql server 12 33
SQL Use Distinct with two fields 3 10
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question