我正在解析从ClinicalTrials.gov下载的xml文件的目录,并且无法提取数据。我可以为单个文件(下面的NCT00006435.xml)执行此操作,但无法弄清楚如何为多个文件执行此操作。用R解析XML文件的目录
library(XML)
# Download ct.gov query and extract xml files
ct<-tempfile()
dir.create("ctdir")
url<-"https://clinicaltrials.gov/search?term=neurofibromatosis-type-1&studyxml=true"
download.file(url, ct)
unzip(ct, exdir="ctdir")
files<-list.files("ctdir")
# Change the working directory so we don't have to worry about the filepath
setwd("ctdir")
# Extract data from one file and get it into a data frame
#xmlfile<-xmlTreeParse("NCT00006435.xml")
#xmltop<-xmlRoot(xmlfile)
#tags<-xmlSApply(xmltop, function(x) xmlSApply(x, xmlValue))
#tags_df<-data.frame(t(tags),row.names=NULL)
# Extract data from each file and get it into a data frame
xmlfiles<-lapply(files,function(x) xmlTreeParse(x))
xmltop<-lapply(xmlfiles,function(x) xmlRoot(x))
tags<-???
如何运行文件列表,循环显示每个文件中的每个标记?
您需要实际下载单个文件。 'xmlTreeParse()'在_local_文件上运行以提取XML。目前,我相信'files'只是包含一个匹配的文件名列表,因为它们出现在服务器上。 –
另外'xmlTreeParse()'不会自动迁移到数据框,但需要'xmlToDataFrame()'。发布示例xml会很有帮助。 – Parfait
Arrgh。 'object.size(xmltop)#40 196 696 bytes'。我们可以有一个“最小”的例子吗?你对'标签'含义的理解是什么? –