将xml文件加载到oracle表时为空行

时间:2014-11-14 14:20:57

标签: xml

请帮我把XML文件加载到oracle表中。    请帮我把XML文件加载到oracle表中。    请帮我把XML文件加载到oracle表中。

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;

请告诉我如何获取数据

Thanks,
Sunil

1 个答案:

答案 0 :(得分:0)

首先是在XML DB Repository中创建资源:

`CREATE OR REPLACE DIRECTORY XMLDIR
AS
'FileFolderPath';`

- 创建资源

DECLARE
 res BOOLEAN;
BEGIN
 res := DBMS_XDB.createResource('/public/myfile.xml',
                             bfilename('XMLDIR', 'myfile.xml'),
                             nls_charset_id('AL32UTF8'));
END;

SELECT x.clamino, x.patname, x.ins1code,
   x.ins1bal, x.ins1priority
FROM XMLTable('for $i in fn:doc("/public/myfile.xml")
             return $i'
            COLUMNS claimno  VARCHAR(10)  PATH 'ATB/CLAIMDATA/Claimno',
                    patname VARCHAR2(10) PATH 'ATB/CLAIMDATA/patname',

ins1code VARCHAR2(10) PATH 'ATB/CLAIMDATA/payors/payordata/inscode[1]',

ins1bal  VARCHAR2(10) PATH 'ATB/CLAIMDATA/payors/payordata/insbal[1]',

ins1priority  VARCHAR2(10) PATH 'ATB/CLAIMDATA/payors/payordata  /inspriority[1]') x;

...等

OR:创建XMLType表并加载表中的文件内容并使用XMLQuery(XQuery)函数

create table mytablename of xmltype;

insert into mytablename  VALUES
(XMLType( BFILENAME('XMLDIR','myfile.xml'), NLS_CHARSET_ID('AL32UTF8')   )); 

select XMLQUERY(for $i ora:view("mytablename")
              RETURNING CONTENT) from dual;