我想从 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 的方法“如何使用嵌套数据框和 purrr 驯服 XML”,但它只适用于第一级。
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
吗?