【发布时间】:2014-05-08 04:26:49
【问题描述】:
我正在尝试从此页面获取所有信息:http://ws.parlament.ch/affairs/19110758/?format=xml
首先我将文件下载到file,然后用xmlParse(file)解析它。
download.file(url = paste0(http://ws.parlament.ch/affairs/19110758/?format=xml), destfile = destfile)
file <- xmlParse(destfile[])
我现在想提取我需要的所有信息。例如标题和 ID 号。我尝试过这样的事情:
title <- xpathSApply(file, "//h2", xmlValue)
但这只会给我一个错误:unable to find an inherited method for function ‘saveXML’ for signature ‘"XMLDocument"
接下来我尝试的是:
library(plyr)
test <-ldply(xmlToList(file), function(x) { data.frame(x[!names(x)=="id"]) } )
这给了我一个data.frame和一些信息。但是我丢失了诸如id 之类的信息(这是最重要的)。
我想得到一个data.frame,其中一行(每个事件只有一行)包含一个事件的所有信息,例如id``updatedadditionalIndexing``affairType等。
有了这个,它就可以工作了(id 的例子):
infofile <- xmlRoot(file)
nodes <- getNodeSet(file, "//affair/id")
id <-as.numeric(lapply(nodes, function(x) xmlSApply(x, xmlValue)))
【问题讨论】:
标签: xml r xml-parsing