【问题标题】:R - Reading multiple xml files with xml2R - 使用 xml2 读取多个 xml 文件
【发布时间】:2020-11-27 17:13:49
【问题描述】:

使用 xml2,我编写了一个代码,它将我正在使用的 xml 文件转换为所需的数据帧。我现在需要对文件夹中的其他 1218 个 xml 文件重复此操作。但是,我正在努力找出从哪里开始。我知道我需要列出文件:

files <- list.files(pattern = ".xml$")   

然后将需要一个循环或 Sapply,但我不确定如何。任何建议将不胜感激。

到目前为止的代码:

 xmlimport <- read_xml("16770601.xml")
        class(xmlimport)
        trialaccounts <- xmlimport %>% xml_find_all('//div1[@type="trialAccount"]')
        defendants=NULL
        for(i in 1:length(trialaccounts)) {
          trialid <- trialaccounts[[i]] %>% xml_attr("id")
          year <- trialaccounts[[i]] %>% xml_find_first('.//interp[@type="year"]') %>% xml_attr("value")
          genderdefendants <- trialaccounts[[i]] %>% 
            xml_find_all('.//persName[@type="defendantName"]/interp[@type="gender"]') %>%
            xml_attr("value")
          descrip <- trialaccounts[[i]] %>% 
            xml_find_all('.//persName[@type="defendantName"]') %>% 
            xml_text(trim=TRUE)
          verdict <- trialaccounts[[i]] %>% 
            xml_find_all('.//interp[@type="verdictCategory"]')%>% xml_attr("value")
          context <- xml_text(trialaccounts[[i]])
          for(j in 1:length(genderdefendants)) { 
            defendants <- defendants %>%
              bind_rows(tibble(defendantid=i,trial_id=trialid,year_tried=year,description=descrip,verdict_result=verdict,info=context,gender=genderdefendants[j]))
          }
        }

【问题讨论】:

    标签: r xml loops xml2


    【解决方案1】:

    我建议编写一个函数来解析一个 xml 并使用包 purrr 将其映射到您的文件列表:

    library(dplyr)
    library(purrr)
    my_xml_reading_function <- function(x) {
      xmlimport <- read_xml(x)
      trialaccounts <- xmlimport %>% xml_find_all('//div1[@type="trialAccount"]')
      defendants=NULL
      for(i in 1:length(trialaccounts)) {
        trialid <- trialaccounts[[i]] %>% xml_attr("id")
        year <- trialaccounts[[i]] %>% xml_find_first('.//interp[@type="year"]') %>% xml_attr("value")
        genderdefendants <- trialaccounts[[i]] %>% 
          xml_find_all('.//persName[@type="defendantName"]/interp[@type="gender"]') %>%
          xml_attr("value")
        descrip <- trialaccounts[[i]] %>% 
          xml_find_all('.//persName[@type="defendantName"]') %>% 
          xml_text(trim=TRUE)
        verdict <- trialaccounts[[i]] %>% 
          xml_find_all('.//interp[@type="verdictCategory"]')%>% xml_attr("value")
        context <- xml_text(trialaccounts[[i]])
        for(j in 1:length(genderdefendants)) { 
          defendants <- defendants %>%
            bind_rows(tibble(defendantid=i,trial_id=trialid,year_tried=year,description=descrip,verdict_result=verdict,info=context,gender=genderdefendants[j]))
        }
      }
      return(defendants)
    }
    
    result <- map(files, ~my_xml_reading_function(.x))
    

    这将为您提供长度为 1218 的列表。您可以使用 result[[1]] 访问第一个结果。或者,如果您想将所有结果合并到一个表中,请使用:

    result <- map_dfr(files, ~my_xml_reading_function(.x))
    

    【讨论】:

    • 嗨,谢谢。这很好用。我只是想知道您是否可以向我解释波浪号在“result
    • 还有 .在 .x 中的行为类似于 .// 用于 xml_find_all 函数?
    • 波浪号注释只是一个更加用户友好的版本,您可以像往常一样调用该函数。在您的情况下,如果您的函数只接受一个参数,map(files, my_xml_reading_function) 也会这样做。 .x 标记将放置files 的任何元素的位置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-06
    • 2019-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多