Extract data from a XML and load it into a table

喜欢而已 提交于 2019-12-11 18:24:44

问题


How would I fetch the data from a XML (loaded into a Oracle table) and create a table to load that extracted data.

I'm trying to create a table, with columns as follows, the below mentioned columns are tags in the xml

ID,  Provider_Name,  Index, Provider_ID_description, Provider_ID
 1,  Provider_P107,  1,     GNRCN,                   GNRCN
 1,  Provider_P107,  2,     INDF1,                   INDF1
 2,  Provider_EGUT,  1,     EGUT,                    EGUT

The XML is:

    <?xml version="1.0" encoding="us-ascii"?>
<AuxiliaryType auxiliaryTypeId="1617309" base="Value list" hasImplementation="false" isShared="true" masteredIn="false" name="ID list" type="String">
    <AuxiliaryObject id="1" name="Provider_P107">
        <Row>
            <Index>1</Index>
            <Provider_ID_description>GNRCN</Provider_ID_description>
            <Provider_ID>GNRCN</Provider_ID>
        </Row>
        <Row>
            <Index>2</Index>
            <Provider_ID_description>INDF1</Provider_ID_description>
            <Provider_ID>INDF1</Provider_ID>
        </Row>
    </AuxiliaryObject>
    <AuxiliaryObject id="2" name="Provider_EGUT">
        <Row>
            <Index>1</Index>
            <Provider_ID_description>EGUT</Provider_ID_description>
            <Provider_ID>EGUT</Provider_ID>
        </Row>
    </AuxiliaryObject>
</AuxiliaryType>

I've tried using the following query:

SELECT w.ID,
       p.Name,
       s.Index,
       q.P_Desc,
       q.P_ID
from  traptabclob t
left join xmltable ('/AuxiliaryType/AuxiliaryObject/'
                   passing t.testclob
                   columns ID      XmlType path '/AuxiliaryType/AuxiliaryObject/@id',
                   columns Name    XmlType path '/AuxiliaryType/AuxiliaryObject/@name',
                   columns Index   XmlType path 'Row/Index',
                   columns P_Desc  varchar2(201) path 'Row/Provider_ID_description',
                   columns P_ID    varchar2(21) path 'Row/Provider_ID',
                   ) q
on (1=1)
  left join xmltable('/Index'
                      passing q.Index
                      columns Index number path '.') s
    on (1=1)
  left join  xmltable('/AuxiliaryType/AuxiliaryObject/'
                        passing q.ID
                        columns ID number path '.') w
    on (1=1)
  left join   xmltable('/AuxiliaryType/AuxiliaryObject/'
                        passing q.Name
                        columns Name varchar2(21) path '.') p
    on (1=1);

I tried this http://www.sqlfiddle.com/#!4/1a672/7/0. Here is the new where I am able to fetch more data http://www.sqlfiddle.com/#!4/1a672/8/0, but I'm still struggling. This does not work when I have more than one index or row. How to do it If I have the count of AuxiliaryObject id ?

来源:https://stackoverflow.com/questions/20866126/extract-data-from-a-xml-and-load-it-into-a-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!