简体   繁体   English

xmlTreeParse不识别树

[英]xmlTreeParse doesnt recognize tree

I have a simple question. 我有一个简单的问题。 I'm trying to get a list of the indicators available on the Open data api. 我正在尝试获取Open Data api上可用的指标列表。 I use the RCurl function getURL to pull the contents of http://api.worldbank.org/indicators , and then the XML function xmlTreeParse on the resulting xml page. 我使用RCurl函数getURL来获取http://api.worldbank.org/indicators的内容,然后在生成的xml页面上获取XML函数xmlTreeParse。 But xmlTreeParse just treats the xml file as a big block of test. 但xmlTreeParse只是将xml文件视为一个重要的测试块。 Why is this? 为什么是这样? Thanks! 谢谢!

library(RCurl)
library(XML)

temp <- getURL("http://api.worldbank.org/indicators)
temp <- xmlTreeParse(temp)

You can use 您可以使用

temp <- getURL("http://api.worldbank.org/indicators")
temp <- xmlParse(temp)
xpathSApply(temp,"//wb:source") # example access data 1
xpathSApply(temp,"//wb:source[@id=2]") # example access data 2

use xmlParse or xmlTreeParse(useInternalNodes=T) 使用xmlParsexmlTreeParse(useInternalNodes=T)

with this simple structure you can transform to a dataframe as follows 使用这种简单的结构,您可以转换为数据帧,如下所示

my.df<-xmlToDataFrame(temp)

or a list 或者一份清单

my.list<-xmlToList(temp)

> my.list[[1]]
$name
[1] "Agricultural machinery, tractors"

$source
$source$text
[1] "World Development Indicators"

$source$.attrs
 id 
"2" 


$sourceNote
[1] "Agricultural machinery refers to the number of wheel and crawler tractors (excluding garden tractors) in use in agriculture at the end of the calendar year specified or during the first quarter of the following year."

$sourceOrganization
[1] "Food and Agriculture Organization, electronic files and web site."

$topics
$topics$topic
$topics$topic$text
[1] "Agriculture & Rural Development  "

$topics$topic$.attrs
 id 
"1" 



$.attrs
              id 
"AG.AGR.TRAC.NO" 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM