Import data from XML files into data.frame I have a number of XML files containing data I would like to analyse. Each XML contains data in a format similar to this:



&lt;?xml version='1.0' encoding='UTF-8'?&gt;
&lt;build&gt;
  &lt;actions&gt;
    ...
  &lt;/actions&gt;
  &lt;queueId&gt;1276&lt;/queueId&gt;
  &lt;timestamp&gt;1447062398490&lt;/timestamp&gt;
  &lt;startTime&gt;1447062398538&lt;/startTime&gt;
  &lt;result&gt;ABORTED&lt;/result&gt;
  &lt;duration&gt;539722&lt;/duration&gt;
  &lt;charset&gt;UTF-8&lt;/charset&gt;
  &lt;keepLog&gt;false&lt;/keepLog&gt;
  &lt;builtOn&gt;&lt;/builtOn&gt;
  &lt;workspace&gt;/var/lib/jenkins/workspace/clean-caches&lt;/workspace&gt;
  &lt;hudsonVersion&gt;1.624&lt;/hudsonVersion&gt;
  &lt;scm class="hudson.scm.NullChangeLogParser"/&gt;
  &lt;culprits class="com.google.common.collect.EmptyImmutableSortedSet"/&gt;
&lt;/build&gt;


These are build.xml files generated by the continuous integration server, Jenkins. The files themselves don't have some important data that I would like, like the Jenkins job name, or the build number that created the xml. The job and build ids are encoded into the path of each file like .\jenkins\jobs\${JOB_NAME}\builds\${BUILD_NUMBER}\build.xml

I would like to create a data frame contain job name, build number, duration, and result.

My code to achieve this is the following:



library(XML)

filenames &lt;- list.files("C:/Users/jenkins/", recursive=TRUE, full.names=TRUE, pattern="build.xml")

job &lt;- unlist(lapply(filenames, function(f) {
    s &lt;- unlist(strsplit(f, split=.Platform$file.sep))
    s[length(s) - 3]
}))

build &lt;- unlist(lapply(filenames, function(f) {
    s &lt;- unlist(strsplit(f, split=.Platform$file.sep))
    s[length(s) - 1]
}))

duration &lt;- unlist(lapply(filenames, function(f) {
    xml &lt;- xmlParse(f)
    xpathSApply(xml, "//duration", xmlValue)
}))

result &lt;- unlist(lapply(filenames, function(f) {
    xml &lt;- xmlParse(f)
    x &lt;- xpathSApply(xml, "//result", xmlValue)
    return(x)
}))

build.data &lt;- data.frame(job, build, result, duration)


Which gives me a data frame that looks like this:




  job             build result     duration
1 clean-caches    37    SUCCESS    248701
2 clean-caches    38    FAILURE    1200049
3 clean-caches    39    FAILURE    1200060
4 clean-caches    40    FAILURE    1200123
5 clean-caches    41    SUCCESS    358024
6 clean-caches    42    SUCCESS    130462



This works, but I have serious concerns about it from both a style and a performance point of view. I'm completely new to R, so I don't know what would be a nicer way to do this.

My concerns:


Repeated code:

The code blocks to generate the job and build vectors are identical. Same for duration and result. If I decide to import more nodes from XML, I'll end up repeating even more code.


Several iterations must be made of my list of files. There are thousands of XML files, and this number will likely grow. As above, if I wish to extract more data from the XML, I must add more iterations.
With a few XML files to read you could have done something like this to address your concerns, where the files are only read at once, but all loaded into memory at the same time:

subdirs &lt;- strsplit(dirname(filenames),
                    split = .Platform$file.sep)
subdirs &lt;- lapply(subdirs, rev)
job   &lt;- sapply(subdirs, `[[`, 3)
build &lt;- sapply(subdirs, `[[`, 1)
xmls &lt;- lapply(filenames, xmlParse)
duration &lt;- sapply(xmls, xpathSApply, "//duration", xmlValue)
result   &lt;- sapply(xmls, xpathSApply, "//result", xmlValue)
build.data &lt;- data.frame(job, build, result, duration)


With thousands of files though, it makes more sense to process the files one by one and only keep the useful information before moving from one file to the next. It also makes sense to write a function to process each file. It could be:

build.info &lt;- function(file, xml_fields = c("duration", "result")) {
   res &lt;- list()
   # process filepath
   subdirs &lt;- rev(unlist(strsplit(dirname(file),
                                  split = .Platform$file.sep)))
   res$job   &lt;- subdirs[[3]]
   res$build &lt;- subdirs[[1]]
   # process xml data
   doc &lt;- xmlTreeParse(file)
   build &lt;- doc$doc$children$build
   res[xml_fields] &lt;- lapply(build[xml_fields], xmlValue)
   # return as a data.frame
   as.data.frame(res)
}


See how the function returns a one row data.frame. Then you can call the function on all files via lapply and bind all the outputs together:

build.data &lt;- do.call(rbind, lapply(filenames, build.info))


With a few changes, you can write a more general function that will take one or more files and do the binding itself (like file.info does)

build.info &lt;- function(file, xml_fields = c("duration", "result")) {
   stopifnot(length(file) &gt; 0L)
   if (length(file) == 1L) {
      res &lt;- list()
      # process filepath
      subdirs &lt;- rev(unlist(strsplit(dirname(file),
                                     split = .Platform$file.sep)))
      res$job   &lt;- subdirs[[1]]
      res$build &lt;- subdirs[[3]]
      # process xml data
      doc &lt;- xmlTreeParse(file)
      build &lt;- doc$doc$children$build
      res[xml_fields] &lt;- lapply(build[xml_fields], xmlValue)
      # return data.frame
      as.data.frame(res)
   } else {
      do.call(rbind, lapply(file, build.info))
   }
}

build.data &lt;- build.info(filenames)