Matlab webread无法从World Databank

时间:2017-11-09 23:34:30

标签: xml matlab api

我尝试使用此命令从World Databank下载经济数据:

DATA = webread('http://api.worldbank.org/countries/all/indicators/NY.GDP.MKTP.CD');

我得到了一大堆XML代码,而不是获取数据。

我该怎么办?

以下是数据库文档:

https://datahelpdesk.worldbank.org/knowledgebase/articles/898599-api-indicator-queries

这是我得到的XML块:

<?xml version="1.0" encoding="utf-8"?>
<wb:indicators page="1" pages="1" per_page="50" total="1" 
xmlns:wb="http://www.worldbank.org">
  <wb:indicator id="NY.GDP.MKTP.CD">
    <wb:name>GDP (current US$)</wb:name>
    <wb:source id="2">World Development Indicators</wb:source>
    <wb:sourceNote>GDP at purchaser's prices is the sum of gross value added by all resident producers in the economy plus any product taxes and minus any subsidies not included in the value of the products. It is calculated without making deductions for depreciation of fabricated assets or for depletion and degradation of natural resources. Data are in current U.S. dollars. Dollar figures for GDP are converted from domestic currencies using single year official exchange rates. For a few countries where the official exchange rate does not reflect the rate effectively applied to actual foreign exchange transactions, an alternative conversion factor is used.</wb:sourceNote>
    <wb:sourceOrganization>World Bank national accounts data, and OECD National Accounts data files.</wb:sourceOrganization>
    <wb:topics>
      <wb:topic id="3">Economy &amp; Growth</wb:topic>
    </wb:topics>
  </wb:indicator>
</wb:indicators>

非常感谢!

1 个答案:

答案 0 :(得分:0)

它对我很有用。当我执行webread API查询时,这就是我得到的:

<?xml version="1.0" encoding="utf-8"?>
 <wb:data page="1" pages="301" per_page="50" total="15048" xmlns:wb="http://www.worldbank.org">
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>2009</wb:date>
     <wb:value>1795472295052.32</wb:value>
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>2008</wb:date>
     <wb:value>2077706582271.06</wb:value>
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>2007</wb:date>
     <wb:value>1637573196970.62</wb:value>
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>2006</wb:date>
     <wb:value>1404113530800.68</wb:value>
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>2005</wb:date>
     <wb:value>1184661549595.13</wb:value>
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>2004</wb:date>
     <wb:value>963862340520.581</wb:value>
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>2003</wb:date>
     <wb:value>823110541431.167</wb:value>
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>2002</wb:date>
     <wb:value>729051715403.984</wb:value>
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>2001</wb:date>
     <wb:value>723282816382.445</wb:value>
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>2000</wb:date>
     <wb:value>735025140336.255</wb:value>
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>1999</wb:date>
     <wb:value>643889670873.196</wb:value>
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>1998</wb:date>
     <wb:value>591525594021.399</wb:value>
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>1997</wb:date>
     <wb:value>613279452474.103</wb:value>
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>1996</wb:date>
     <wb:value>578231297763.478</wb:value>
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>1995</wb:date>
     <wb:value>523759898288.293</wb:value>
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>1994</wb:date>
     <wb:value>487526906280.526</wb:value>
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>1993</wb:date>
     <wb:value>476513630630.268</wb:value>
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>1992</wb:date>
     <wb:value>471163515480.256</wb:value>
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>1991</wb:date>
     <wb:value>439779177858.364</wb:value>
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>1990</wb:date>
     <wb:value>446877161735.167</wb:value>
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>1989</wb:date>
     <wb:value>322325140494.687</wb:value>
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>1988</wb:date>
     <wb:value>307503035642.479</wb:value>
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>1987</wb:date>
     <wb:value>312681678329.895</wb:value>
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>1986</wb:date>
     <wb:value>289029150639.408</wb:value>
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>1985</wb:date>
     <wb:value>303893618415.506</wb:value>
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>1984</wb:date>
     <wb:value>307940771858.505</wb:value>
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>1983</wb:date>
     <wb:value>303962539723.043</wb:value>
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>1982</wb:date>
     <wb:value>324328753765.261</wb:value>
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>1981</wb:date>
     <wb:value>348592795413.075</wb:value>
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>1980</wb:date>
     <wb:value>338177454717.499</wb:value>
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>1979</wb:date>
     <wb:value>248646206366.973</wb:value>
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>1978</wb:date>
     <wb:value>183555544342.167</wb:value>
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>1977</wb:date>
     <wb:value>167308327683.573</wb:value>
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>1976</wb:date>
     <wb:value>144846175400.471</wb:value>
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>1975</wb:date>
     <wb:value>116337021938.327</wb:value>
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>1974</wb:date>
     <wb:value>105145803084.364</wb:value>
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>1973</wb:date>
     <wb:value>55018394945.576</wb:value>
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>1972</wb:date>
     <wb:value>43316056615.451</wb:value>
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>1971</wb:date>
     <wb:value>36426909888.3884</wb:value>
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>1970</wb:date>
     <wb:value>31385499664.0635</wb:value>
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>1969</wb:date>
     <wb:value>28434203615.4795</wb:value>
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>1968</wb:date>
     <wb:value>25760683041.0826</wb:value>
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>1967</wb:date>
     <wb:value />
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>1966</wb:date>
     <wb:value />
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>1965</wb:date>
     <wb:value />
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>1964</wb:date>
     <wb:value />
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>1963</wb:date>
     <wb:value />
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>1962</wb:date>
     <wb:value />
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>1961</wb:date>
     <wb:value />
     <wb:decimal>0</wb:decimal>
   </wb:data>
   <wb:data>
     <wb:indicator id="NY.GDP.MKTP.CD">GDP (current US$)</wb:indicator>
     <wb:country id="1A">Arab World</wb:country>
     <wb:date>1960</wb:date>
     <wb:value />
     <wb:decimal>0</wb:decimal>
   </wb:data>
 </wb:data>

无论如何,一旦阅读完毕,您必须对数据进行清理,然后将其解析为XML。因此:

res = webread('http://api.worldbank.org/countries/all/indicators/NY.GDP.MKTP.CD');
res = unicode2native(res);
res = native2unicode(res,'UTF-8');

% If the xmlread function throws a SAXParseException in the form of "content is not allowed in prolog", use the following like of code to get rid of the BOM garbage at the beginning of the string, before the XML declaration. Otherwise, just comment this and proceed.
res = res(2:end);

iobj = java.io.StringBufferInputStream(res);
tobj = xmlread(iobj);

您将收到的字符串解码为字节,然后将其重新编码为UTF-8以确保使用正确的编码(此处的XML解析器需要UTF-8编码的字符串)。如果字符串开头的BOM垃圾使您的函数失败,请将其删除。获得XML树之后,只需迭代其节点并选择所需的数据。