https://github.com/cran/XML
Raw File
Tip revision: 6ac5aacfa6b8250c6501b2fe1c24fc9f287f02f3 authored by Duncan Temple Lang on 29 April 2011, 00:00:00 UTC
version 3.4-0
Tip revision: 6ac5aac
metlin.R
readMetlin =
function(url = "http://metlin.scripps.edu/download/MSMS_test.XML",
         what = c("molid" = "integer", name = "character", formula = "character", mass = "numeric", mz = "numeric"))
{  

   doc = xmlTreeParse(url, useInternal = TRUE)
   z = xmlChildren(xmlRoot(doc))
   nodes = z[ sapply(z, inherits, "XMLInternalElementNode") ]

   if(length(nodes) > 1) {

   }

   nodes = nodes[[1]]
   n = xmlSize(nodes)
   ans = as.data.frame(lapply(what, function(x) get(x)(n)))

   for(i in 1:n) {
     node = nodes[[i]]
     sapply(names(what),
               function(id) {
                    ans[i, id] <<- as(xmlValue(node[[id]]), what[id])
              })
   }

   ans
 }

back to top