我想读取多个xml文件并将结果写入CSV文件。 我的XMl文件如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<testsuites time="430.819">
<testsuite name="Notebook.R notebook opening as a Anonymous user" tests="8" failures="1" errors="0" time="152.319" timestamp="2016-06-27T06:22:33.708Z" package="anonymous_user/tc_52.1.1">
<testcase name="Github page has been loaded" classname="anonymous_user/tc_52.1.1" time="22.002"/>
<testcase name="the element Shareable Link exists" classname="anonymous_user/tc_52.1.1" time="32.666"/>
<testcase name="Logout button exists" classname="anonymous_user/tc_52.1.1" time="5.1"/>
<testcase name="the element Shareable Link exists" classname="anonymous_user/tc_52.1.1" time="16.111"/>
<testcase name="Logout button exists" classname="anonymous_user/tc_52.1.1" time="5.093"/>
<testcase name="Fork option exists" classname="anonymous_user/tc_52.1.1" time="2.058"/>
<testcase name="Fork option clicked" classname="anonymous_user/tc_52.1.1" time="0.028"/>
<testcase name="Required element found hence "Notebook.R" notebook opened successfully" classname="anonymous_user/tc_52.1.1" time="69.261">
<failure type="assertExists">Required element found hence "Notebook.R" notebook opened successfully</failure>
</testcase>
<system-out></system-out>
</testsuite>
<testsuite name="Mini.html notebook opening as a Anonymous user" tests="8" failures="1" errors="0" time="139.257" timestamp="2016-06-27T06:22:33.710Z" package="anonymous_user/tc_52.2.1">
<testcase name="RCloud Home page already loaded" classname="anonymous_user/tc_52.2.1" time="24.321"/>
<testcase name="the element Shareable Link exists" classname="anonymous_user/tc_52.2.1" time="19.568"/>
<testcase name="Logout button exists" classname="anonymous_user/tc_52.2.1" time="5.102"/>
<testcase name="the element Shareable Link exists" classname="anonymous_user/tc_52.2.1" time="16.021"/>
<testcase name="Logout button exists" classname="anonymous_user/tc_52.2.1" time="5.099"/>
<testcase name="Fork option exists" classname="anonymous_user/tc_52.2.1" time="2.07"/>
<testcase name="Fork option clicked" classname="anonymous_user/tc_52.2.1" time="0.045"/>
<testcase name="Required element found hence "Mini.html" notebook opened successfully" classname="anonymous_user/tc_52.2.1" time="67.031">
<failure type="assertExists">Required element found hence "Mini.html" notebook opened successfully</failure>
</testcase>
<system-out></system-out>
</testsuite>
<testsuite name="Shiny.html notebook opening as a Anonymous user" tests="8" failures="1" errors="0" time="139.243" timestamp="2016-06-27T06:22:33.711Z" package="anonymous_user/tc_52.3.1">
<testcase name="RCloud Home page already loaded" classname="anonymous_user/tc_52.3.1" time="24.008"/>
<testcase name="the element Shareable Link exists" classname="anonymous_user/tc_52.3.1" time="19.571"/>
<testcase name="Logout button exists" classname="anonymous_user/tc_52.3.1" time="5.101"/>
<testcase name="the element Shareable Link exists" classname="anonymous_user/tc_52.3.1" time="16.104"/>
<testcase name="Logout button exists" classname="anonymous_user/tc_52.3.1" time="5.082"/>
<testcase name="Fork option exists" classname="anonymous_user/tc_52.3.1" time="2.063"/>
<testcase name="Fork option clicked" classname="anonymous_user/tc_52.3.1" time="0.017"/>
<testcase name="Required element found hence "shiny.html" notebook opened successfully" classname="anonymous_user/tc_52.3.1" time="67.297">
<failure type="assertExists">Required element found hence "shiny.html" notebook opened successfully</failure>
</testcase>
<system-out></system-out>
</testsuite>
</testsuites>
以下是R脚本
# Script to Parse the XMLfiles
# install.packages("XML")
library("XML")
path = "File path" # set the location of xml files
file.names <- dir(path, pattern =".xml")
print(file.names)
D=data.frame("Package", 'Name',"Time", "Failures",'Status')
for (k in 1:length(file.names))
{
doc<-xmlTreeParse(file.names[k])
top<-xmlRoot(doc)
z=names(top)
testcases<-length(z)
package=c()
name=c()
failures=c()
time=c()
for (i in 1:length(z))
{
b<-xmlAttrs(top[[i]])
package=c(package,b['package'])
name=c(name,b['name'])
failures=c(failures, b['failures'])
time=c(time,b['time'])
}
status=c()
df<-data.frame(package, name, time, failures)
status<-function(val){
if (val == 0 ){
status=c(status,'Pass')
}
else{
status=c(status,'Fail')
}
}
lapply(list(df['failures']), status)
status<-function(val){
if (val != 0 ){
return ('Fail')
}
else{
return ('Pass')
}
}
status<-apply(df['failures'],1,FUN= status)
df<-cbind(df,status)
D=rbind(as.matrix(D),as.matrix(df))
print(df)
}
# warnings()
write.csv(file="result.csv",D)
当我运行上面的R脚本时,它显示以下错误:
错误:XML内容似乎不是XML:&#39;目录中的第一个xml文件名&#39;
注意:我的本地计算机上存在所有XML文件,R版本为3.3.1,我也安装了所需的软件包。
因为我是R的新人,请帮帮我