gpt4 book ai didi

r - 如何使用 xml2 和 purrr 提取不同级别的 xml_attr 和 xml_text?

转载 作者:行者123 更新时间:2023-12-02 20:29:11 26 4
gpt4 key购买 nike

我想从 XML 文件中提取信息并将其转换为数据帧。

信息以 XML 文本和 XML 属性的形式存储在嵌套节点中:

示例结构:

<xmlnode node-id = "Text about xmlnode">
<xmlsubnode subnode-id = "123">
<xmlsubsubnode>
I want to extract this text
</xmlsubsubnode>
<xmlsubsubnode>
I want to extract this text
</xmlsubsubnode>
<xmlsubsubnode>
I want to extract this text
</xmlsubsubnode>
<xmlsubsubnode>
I want to extract this text
</xmlsubsubnode>
</xmlsubnode>
<xmlsubnode subnode-id = "456">
<xmlsubsubnode>
I want to extract this text
</xmlsubsubnode>
<xmlsubsubnode>
I want to extract this text
</xmlsubsubnode>
<xmlsubsubnode>
I want to extract this text
</xmlsubsubnode>
<xmlsubsubnode>
I want to extract this text
</xmlsubsubnode>
</xmlsubnode>
</xmlnode>
<xmlnode node-id = "Text about xmlnode">
<xmlsubnode subnode-id = "123">
<xmlsubsubnode>
I want to extract this text
</xmlsubsubnode>
<xmlsubsubnode>
I want to extract this text
</xmlsubsubnode>
<xmlsubsubnode>
I want to extract this text
</xmlsubsubnode>
<xmlsubsubnode>
I want to extract this text
</xmlsubsubnode>
</xmlsubnode>
<xmlsubnode subnode-id = "456">
<xmlsubsubnode>
I want to extract this text
</xmlsubsubnode>
<xmlsubsubnode>
I want to extract this text
</xmlsubsubnode>
<xmlsubsubnode>
I want to extract this text
</xmlsubsubnode>
<xmlsubsubnode>
I want to extract this text
</xmlsubsubnode>
</xmlsubnode>
</xmlnode>

我想获取这些信息:

* node-id (attribute)
* subnode-id (attribute)
* text in `xmlsubnodenode` (text)

我需要一个像这样的长格式数据框:

node-id subnode-id  text
Text about xmlnode 1 123 I want to extract this text
Text about xmlnode 1 123 I want to extract this text
Text about xmlnode 1 123 I want to extract this text
Text about xmlnode 1 123 I want to extract this text
Text about xmlnode 1 456 I want to extract this text
Text about xmlnode 1 456 I want to extract this text
Text about xmlnode 1 456 I want to extract this text
Text about xmlnode 1 456 I want to extract this text
Text about xmlnode 2 123 I want to extract this text
Text about xmlnode 2 123 I want to extract this text
Text about xmlnode 2 123 I want to extract this text
Text about xmlnode 2 123 I want to extract this text
Text about xmlnode 2 456 I want to extract this text
Text about xmlnode 2 456 I want to extract this text
Text about xmlnode 2 456 I want to extract this text
Text about xmlnode 2 456 I want to extract this text

我尝试遵循 Jenny Bryans 的方法 "How to tame XML with nested data frames and purrr" ,但它只适用于第一级。

xml <- xml2::read_xml("input/example.xml")
rows <-
xml %>%
xml_find_all("//xmlnode")
rows_df <- data_frame(row = seq_along(rows), nodeset = rows)
rows_df %>%
mutate(node_id = nodeset %>% map(~ xml_attr(., "node-id"))) %>%
select(row, node_id) %>%
unnest()

您有想法通过 purrr 获取这些信息吗?

最佳答案

无需展开/向另一个数据帧添加行的方法:创建一个包含每个 subsubnode 行的数据帧,并将 purrrxml2 选择并提取 xmlsubnode 父级和 xmlnode 祖先的值。

工作示例:

library(dplyr)
library(xml2)
library(purrr)
library(tidyr)
xml <- xml2::read_xml("input/example.xml")
rows <- xml %>% xml_find_all("//xmlsubsubnode")
rows_df <- data_frame(node = rows) %>%
mutate(node_id = node %>% map(~ xml_find_first(., "ancestor::xmlnode")) %>% map(~ xml_attr(., "node-id"))) %>%
mutate(subnode_id = node %>% map(~ xml_parent(.)) %>% map(~ xml_attr(., "subnode-id"))) %>%
mutate(text = node %>% map(~ xml_text(.))) %>%
select(-node)

关于r - 如何使用 xml2 和 purrr 提取不同级别的 xml_attr 和 xml_text?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49253021/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com