基于R下载和阅读压缩XML文件(Using R to download and read zippe

2019-10-20 14:09发布

基于此答案由德克Eddelbuettel我试图读取xml从文件zip作进一步处理归档。 除了URL和文件名,以引用的代码的唯一的变化是,我改变read.tablexmlInternalTreeParse

library(XML)
temp <- tempfile()
download.file("http://epp.eurostat.ec.europa.eu/NavTree_prod/everybody/BulkDownloadListing?sort=1&downfile=data%2Fnrg_105a.sdmx.zip",temp)
doc <- xmlInternalTreeParse(unz(temp, "nrg_105a.dsd.xml"))
fileunlink(temp)
closeAllConnections()

然而,这将返回以下错误:

Error in file.exists(file) : invalid 'file' argument

traceback()表明,这是从分析器内的函数调用。 所以温度似乎是在这种情况下不恰当的引用。 有没有一种方法,使这项工作?

Answer 1:

你可以试试:

# Make a temporary file (tf) and a temporary folder (tdir)
tf <- tempfile(tmpdir = tdir <- tempdir())

## Download the zip file 
download.file("http://epp.eurostat.ec.europa.eu/NavTree_prod/everybody/BulkDownloadListing?sort=1&downfile=data%2Fnrg_105a.sdmx.zip", tf)

## Unzip it in the temp folder
xml_files <- unzip(tf, exdir = tdir)

## Parse the first file
doc <- xmlInternalTreeParse(xml_files[1])

## Delete temporary files
unlink(tdir, T, T)


文章来源: Using R to download and read zipped xml file
标签: xml r zip