Storing specific XML node values with R's xmlEventParse

后端 未结 3 1475
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-09 13:17

I have a big XML file which I need to parse with xmlEventParse in R. Unfortunately on-line examples are more complex than I need, and I just want to flag a matching node tag

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-09 13:46

    For others who may try to lear from M.Morgan - here is the complete code

    fileName = system.file("exampleData", "mtcars.xml", package = "XML")
    
    ourBranches <- function() {
      store <- new.env() 
      record <- function(x, ...) {
        key <- xmlAttrs(x)[["id"]]
        value <- xmlValue(x)
        store[[key]] <- value
      }
      getStore <- function() as.list(store)
      list(record=record, getStore=getStore)
    }
    
    branches <- ourBranches()
    xmlEventParse(fileName, list(), branches=branches)
    head(branches$getStore(), 2)
    

提交回复
热议问题