R - 使用 xml2 读取多个 xml 文件
R - Reading multiple xml files with xml2
我用 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]))
}
}
我建议编写一个函数来解析一个 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]]
访问第一个结果。或者,如果您想将所有结果合并为一个 table,请使用:
result <- map_dfr(files, ~my_xml_reading_function(.x))
我用 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]))
}
}
我建议编写一个函数来解析一个 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]]
访问第一个结果。或者,如果您想将所有结果合并为一个 table,请使用:
result <- map_dfr(files, ~my_xml_reading_function(.x))