使用 Rvest 将特定父节点的所有子节点的文本提取为数据框中的变量
Extract text of all child nodes of a specific parent node as a variable in a data frame using Rvest
我有一个 xml 结构如下的文档
<ClassificationNode>
<District id = "8">
<code>1A</code>
<Name>LALD1</Name>
<Zone id = "21254">
<E1>OU29</E1>
</Zones>
</District>
<ClassificationNodeChildList count = "2">
<ClassificationNode>
<District id = "8.1">
<code>1B</code>
<Name>LALD1A</Name>
<Zone id = "213">
<E1>OU54</E1>
</Zones>
</District>
<District id = "8.2">
<code>1C</code>
<Name>LALD1C</Name>
<Zone id = "214">
<E1>OU65</E1>
</Zones>
</District>
</ClassificationNode>
</ClassificationNodeChildList>
</ClassificationNode>
我想要一个如下所示的数据框:
district_id code Name Zone_id E1
8 1A LALD1 21254 OU29
8.1 1B LALD1A 213 OU54
8.2 1B LALD1B 214 OU65
我想尽可能避免循环。看起来这会非常简单(而且可能是),但我很难过。
我试过:
library(rvest)
library(tidyverse)
x <- "pathtolocalxmlfile.xml"
district_id <- x %>%
read_xml() %>%
xml_find_all('//District') %>%
xml_attrs() %>%
data.frame()
这给了我地区 ID 和
Nodes <- x %>%
read_xml() %>%
xml_find_all('//District') %>%
xml_text()
给我区节点下每个子节点的文本,但作为折叠的字符串。关于如何有效地将数据强制转换为数据框的任何想法。
提前致谢!
首先,您以标签 Zone
开始但以 Zones
结束 -> 将结束标签修改为 Zone
。
然后阅读并实现这段代码:
library(rvest)
pg <- read_xml("pathtolocalxmlfile.xml")
district_id <- pg %>% xml_find_all("//District") %>% xml_attr("id")
code <- pg %>% xml_find_all("//District/code") %>% xml_text(trim = TRUE)
name <- pg %>% xml_find_all("//District/Name") %>% xml_text(trim = TRUE)
zone_id <- pg %>% xml_find_all("//District/Zone") %>% xml_attr("id")
E1 <- pg %>% xml_find_all("//District/Zone/E1") %>% xml_text(trim = TRUE)
df <- data.frame(district_id, code, name, zone_id, E1)
输出:
> df
district_id code name zone_id E1
1 8 1A LALD1 21254 OU29
2 8.1 1B LALD1A 213 OU54
3 8.2 1C LALD1C 214 OU65
我有一个 xml 结构如下的文档
<ClassificationNode>
<District id = "8">
<code>1A</code>
<Name>LALD1</Name>
<Zone id = "21254">
<E1>OU29</E1>
</Zones>
</District>
<ClassificationNodeChildList count = "2">
<ClassificationNode>
<District id = "8.1">
<code>1B</code>
<Name>LALD1A</Name>
<Zone id = "213">
<E1>OU54</E1>
</Zones>
</District>
<District id = "8.2">
<code>1C</code>
<Name>LALD1C</Name>
<Zone id = "214">
<E1>OU65</E1>
</Zones>
</District>
</ClassificationNode>
</ClassificationNodeChildList>
</ClassificationNode>
我想要一个如下所示的数据框:
district_id code Name Zone_id E1
8 1A LALD1 21254 OU29
8.1 1B LALD1A 213 OU54
8.2 1B LALD1B 214 OU65
我想尽可能避免循环。看起来这会非常简单(而且可能是),但我很难过。
我试过:
library(rvest)
library(tidyverse)
x <- "pathtolocalxmlfile.xml"
district_id <- x %>%
read_xml() %>%
xml_find_all('//District') %>%
xml_attrs() %>%
data.frame()
这给了我地区 ID 和
Nodes <- x %>%
read_xml() %>%
xml_find_all('//District') %>%
xml_text()
给我区节点下每个子节点的文本,但作为折叠的字符串。关于如何有效地将数据强制转换为数据框的任何想法。
提前致谢!
首先,您以标签 Zone
开始但以 Zones
结束 -> 将结束标签修改为 Zone
。
然后阅读并实现这段代码:
library(rvest)
pg <- read_xml("pathtolocalxmlfile.xml")
district_id <- pg %>% xml_find_all("//District") %>% xml_attr("id")
code <- pg %>% xml_find_all("//District/code") %>% xml_text(trim = TRUE)
name <- pg %>% xml_find_all("//District/Name") %>% xml_text(trim = TRUE)
zone_id <- pg %>% xml_find_all("//District/Zone") %>% xml_attr("id")
E1 <- pg %>% xml_find_all("//District/Zone/E1") %>% xml_text(trim = TRUE)
df <- data.frame(district_id, code, name, zone_id, E1)
输出:
> df
district_id code name zone_id E1
1 8 1A LALD1 21254 OU29
2 8.1 1B LALD1A 213 OU54
3 8.2 1C LALD1C 214 OU65