Error: XML content does not seem to be XML | for R version 3.3.1 -
i want read multiple xml files , write results csv file. xml files looks below:
<?xml version="1.0" encoding="utf-8"?> <testsuites time="430.819"> <testsuite name="notebook.r notebook opening 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 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 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 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 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>
and below r script
# script parse xmlfiles # install.packages("xml") library("xml") path = "file path" # set location of xml files file.names <- dir(path, pattern =".xml") print(file.names) d=data.frame("package", 'name',"time", "failures",'status') (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() (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)
when run above r scripts shows following error:
error: xml content not seem xml: '1st xml file name directory'
note: xml files present on local machine , r version 3.3.1 , have installed required package.
since new r please me
library("xml")
get file list current directory .xml
extension. parse xml file using xmlparse
, create xpath expression - xpexpr
allow find nodes , loop through these nodes using xpathsapply
, xml attributes using xmlattrs
in form of matrix. convert data frame , store csv file using write.csv
in current directory.
file_list <- list.files(path = ".", pattern = "*.xml", full.names = true) for(i in file_list){ doc <- xmlparse(i) xpexpr <- "//testsuites/testsuite" write.csv(x = data.frame(xpathsapply(doc, xpexpr, xmlattrs), stringsasfactors = false), file = sub(".xml", ".csv", i)) }
to verify, read csv file.
read.csv("./temp.csv")
if want list of data frames having attributes in each testsuite/testcase
node, use following.
doc <- xmlparse("temp.xml") xpexpr <- "//testsuites/testsuite/@name" testsuite_names <- xpathsapply(doc, xpexpr) dflist <- lapply(testsuite_names, function(x) {xpexpr <- paste("//testsuite[contains(@name, ", "'", x, "'", ")]/testcase", sep = ""); data.frame(xpathsapply(doc, xpexpr, xmlattrs), stringsasfactors = false)}) str(dflist) # list of 3 # $ :'data.frame': 3 obs. of 8 variables: # ..$ x1: chr [1:3] "github page has been loaded" "anonymous_user/tc_52.1.1" "22.002" # ..$ x2: chr [1:3] "the element shareable link exists" "anonymous_user/tc_52.1.1" "32.666" # ..$ x3: chr [1:3] "logout button exists" "anonymous_user/tc_52.1.1" "5.1" # ..$ x4: chr [1:3] "the element shareable link exists" "anonymous_user/tc_52.1.1" "16.111" # ..$ x5: chr [1:3] "logout button exists" "anonymous_user/tc_52.1.1" "5.093" # ..$ x6: chr [1:3] "fork option exists" "anonymous_user/tc_52.1.1" "2.058" # ..$ x7: chr [1:3] "fork option clicked" "anonymous_user/tc_52.1.1" "0.028" # ..$ x8: chr [1:3] "required element found hence \"notebook.r\" notebook opened successfully" "anonymous_user/tc_52.1.1" "69.261" # $ :'data.frame': 3 obs. of 8 variables: # ..$ x1: chr [1:3] "rcloud home page loaded" "anonymous_user/tc_52.2.1" "24.321" # ..$ x2: chr [1:3] "the element shareable link exists" "anonymous_user/tc_52.2.1" "19.568" # ..$ x3: chr [1:3] "logout button exists" "anonymous_user/tc_52.2.1" "5.102" # ..$ x4: chr [1:3] "the element shareable link exists" "anonymous_user/tc_52.2.1" "16.021" # ..$ x5: chr [1:3] "logout button exists" "anonymous_user/tc_52.2.1" "5.099" # ..$ x6: chr [1:3] "fork option exists" "anonymous_user/tc_52.2.1" "2.07" # ..$ x7: chr [1:3] "fork option clicked" "anonymous_user/tc_52.2.1" "0.045" # ..$ x8: chr [1:3] "required element found hence \"mini.html\" notebook opened successfully" "anonymous_user/tc_52.2.1" "67.031" # $ :'data.frame': 3 obs. of 8 variables: # ..$ x1: chr [1:3] "rcloud home page loaded" "anonymous_user/tc_52.3.1" "24.008" # ..$ x2: chr [1:3] "the element shareable link exists" "anonymous_user/tc_52.3.1" "19.571" # ..$ x3: chr [1:3] "logout button exists" "anonymous_user/tc_52.3.1" "5.101" # ..$ x4: chr [1:3] "the element shareable link exists" "anonymous_user/tc_52.3.1" "16.104" # ..$ x5: chr [1:3] "logout button exists" "anonymous_user/tc_52.3.1" "5.082" # ..$ x6: chr [1:3] "fork option exists" "anonymous_user/tc_52.3.1" "2.063" # ..$ x7: chr [1:3] "fork option clicked" "anonymous_user/tc_52.3.1" "0.017" # ..$ x8: chr [1:3] "required element found hence \"shiny.html\" notebook opened successfully" "anonymous_user/tc_52.3.1" "67.297"
Comments
Post a Comment