如何在Oracle SQL中解析XML文件

时间:2013-01-23 22:51:06

标签: sql xml json oracle

我从以下网站下载了每日交换文件到数据库中。如何使用oracle SQL解析以下XML文档?

  

“http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote;currency=true?view=basic&format=json”

2 个答案:

答案 0 :(得分:3)

如果你真的下载了XML版本而不是JSON版本,你可以形成如下基本查询:

select /*+ cursor_sharing_exact */ id, classname, name, value
  from (select rownum id, classname, field_list
          from myxml mx cross join
               xmltable('/list/resources/resource'
                        passing xmltype(mx.mydoc)
                        columns classname  varchar2(100) path '@classname',
                        field_list xmltype path 'field'

                       )
       ) 
       cross join xmltable('/field'
                    passing field_list
                    columns
                    name varchar2(200) path '@name',
                    value varchar2(200) path '.'
                   )

这会给你输出:

SQL> select /*+ cursor_sharing_exact */ id, classname, name, value
  2    from (select rownum id, classname, field_list
  3            from myxml mx cross join
  4                 xmltable('/list/resources/resource'
  5                          passing xmltype(mx.mydoc)
  6                          columns classname  varchar2(100) path '@classname',
  7                          field_list xmltype path 'field'
  8                         )
  9         )
 10         cross join xmltable('/field'
 11                      passing field_list
 12                      columns
 13                      name varchar2(200) path '@name',
 14                      value varchar2(200) path '.'
 15                     )
 16  /

        ID CLASSNAME            NAME                 VALUE
---------- -------------------- -------------------- --------------------
         1 Quote                change               0.099976
         1 Quote                chg_percent          0.009367
         1 Quote                name                 USD/KRW
         1 Quote                price                1067.400024
         1 Quote                symbol               KRW=X
         1 Quote                ts                   1359020860
         1 Quote                type                 currency
         1 Quote                volume               0

你当然可以躲过它。

with data as (select /*+ cursor_sharing_exact */ id, classname, name, value
                from (select rownum id, classname, field_list
                        from myxml mx cross join
                             xmltable('/list/resources/resource'
                                      passing xmltype(mx.mydoc)
                                      columns classname  varchar2(100) path '@classname',
                                      field_list xmltype path 'field'
                                     )
                     ) 
                     cross join xmltable('/field'
                                  passing field_list
                                  columns
                                  name varchar2(200) path '@name',
                                  value varchar2(200) path '.'
                                 ) ) select id, classname,
       max(case when name = 'change' then value end) change,
       max(case when name = 'chg_percent' then value end) chg_percent,
       max(case when name = 'name' then value end) name,
       max(case when name = 'price' then value end) price,
       max(case when name = 'symbol' then value end) symbol,
       max(case when name = 'ts' then value end) ts,
       max(case when name = 'type' then value end) type,
       max(case when name = 'volume' then value end) volume   
 from data  
group by id, classname 
order by id

样本(缩短数据):http://sqlfiddle.com/#!4/7b53c/1

答案 1 :(得分:0)

如果要解析JSON文件,PL / JSON库可以为您提供帮助。 Pl/JSON site