Solved

xml to Oracle Database

Posted on 2014-11-17
3
29 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

896 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now