【问题标题】:xmlTreeParse doesnt recognize treexmlTreeParse 无法识别树
【发布时间】:2012-06-19 21:22:16
【问题描述】:

我有一个简单的问题。我正在尝试获取 Open data api 上可用的指标列表。我使用 RCurl 函数 getURL 来拉取http://api.worldbank.org/indicators 的内容,然后在生成的 xml 页面上使用 XML 函数 xmlTreeParse。但是 xmlTreeParse 只是将 xml 文件视为一大块测试。为什么是这样?谢谢!

library(RCurl)
library(XML)

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

【问题讨论】:

    标签: xml r api xml-parsing


    【解决方案1】:

    你可以使用

    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
    

    使用xmlParsexmlTreeParse(useInternalNodes=T)

    通过这种简单的结构,您可以按如下方式转换为数据框

    my.df<-xmlToDataFrame(temp)
    

    或列表

    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" 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-02
      • 2018-10-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多