[英]How to parse XML file in Oracle SQL
I downloaded an daily exchange file from the below website into the database. 我从下面的网站下载了每日交换文件到数据库中。 How do I parse following XML document using oracle SQL?
如何使用Oracle SQL解析以下XML文档?
"http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote;currency=true?view=basic&format=json"
“ http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote;currency=true?view=basic&format=json”
if you've really downloaded the XML version and not the JSON one, you can form the basic query like: 如果您确实下载了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 '.'
)
this would give you output like: 这将为您提供如下输出:
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
you could PIVOT that of course. 您当然可以选择PIVOT。
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
a sample (shortened data): http://sqlfiddle.com/#!4/7b53c/1 一个样本(缩短的数据): http : //sqlfiddle.com/#!4/7b53c/1
If you want to parse JSON files the PL/JSON library can help you. 如果您想解析JSON文件,PL / JSON库可以为您提供帮助。 Pl/JSON site
PL / JSON网站
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.