如何将Rinto中的复杂xml解析为数据帧?

如何将Rinto中的复杂xml解析为数据帧?,r,xml,R,Xml,我想解析一个嵌套的xml文件,其布局如下R所示,并将其加载到数据帧中。我尝试使用了几个eay,包括xml和xml2包,但都没有成功 <?xml version="1.0" encoding="UTF-8"?> <Targets> <Target TYPE="myserver.mgmt.Metric" NAME="metric1"> <Attribute NAME="name" VALUE="metric1"></Attribute> &

我想解析一个嵌套的xml文件,其布局如下R所示,并将其加载到数据帧中。我尝试使用了几个eay,包括xml和xml2包,但都没有成功

<?xml version="1.0" encoding="UTF-8"?>
<Targets>
<Target TYPE="myserver.mgmt.Metric" NAME="metric1">
<Attribute NAME="name" VALUE="metric1"></Attribute>
<Attribute NAME="Value" VALUE="2.4"></Attribute>
<Attribute NAME="collectionTime" VALUE="1525118288000"></Attribute>
<Attribute NAME="State" VALUE="normal"></Attribute>
<Attribute NAME="ObjectName" VALUE="obj1"></Attribute>
<Attribute NAME="ValueHistory" VALUE="5072"></Attribute>
</Target>
...
<Target TYPE="myserver.mgmt.Metric" NAME="metric999">
<Attribute NAME="name" VALUE="metric999"></Attribute>
<Attribute NAME="Value" VALUE="60.35"></Attribute>
<Attribute NAME="collectionTime" VALUE="1525118288000"></Attribute>
<Attribute NAME="State" VALUE="normal"></Attribute>
<Attribute NAME="ObjectName" VALUE="obj1"></Attribute>
<Attribute NAME="ValueHistory" VALUE="9550"></Attribute>
</Target>
</Targets>

非常感谢您的帮助。

我们可以将
XML
tidyverse

library(XML)
library(tidyverse)
lst1 <- getNodeSet(xml1, path = "//Target")
map_df(seq_along(lst1), ~ 
     XML:::xmlAttrsToDataFrame(lst1[[.x]])  %>% 
        mutate_all(as.character) %>%
        deframe %>%
        as.list %>% 
        as_tibble) %>%
        mutate_all(type.convert, as.is = TRUE)
# A tibble: 2 x 6
#  name      Value collectionTime State  ObjectName ValueHistory
#  <chr>     <dbl>          <dbl> <chr>  <chr>             <int>
#1 metric1     2.4  1525118288000 normal obj1               5072
#2 metric999  60.4  1525118288000 normal obj1               9550
库(XML)
图书馆(tidyverse)
1%
全部变异(如字符)%>%
碎片整理%>%
as.list%>%
作为可测试的)%>%
mutate_all(type.convert,as.is=TRUE)
#一个tibble:2x6
#名称值集合时间状态对象名称值历史记录
#                                 
#1公制1 2.4 1525118288000正常obj1 5072
#2公制999 60.4 1525118288000正常obj1 9550
数据
xml1
library(XML)
library(tidyverse)
lst1 <- getNodeSet(xml1, path = "//Target")
map_df(seq_along(lst1), ~ 
     XML:::xmlAttrsToDataFrame(lst1[[.x]])  %>% 
        mutate_all(as.character) %>%
        deframe %>%
        as.list %>% 
        as_tibble) %>%
        mutate_all(type.convert, as.is = TRUE)
# A tibble: 2 x 6
#  name      Value collectionTime State  ObjectName ValueHistory
#  <chr>     <dbl>          <dbl> <chr>  <chr>             <int>
#1 metric1     2.4  1525118288000 normal obj1               5072
#2 metric999  60.4  1525118288000 normal obj1               9550
xml1 <- xmlParse('<?xml version="1.0" encoding="UTF-8"?>
<Targets>
<Target TYPE="myserver.mgmt.Metric" NAME="metric1">
<Attribute NAME="name" VALUE="metric1"></Attribute>
<Attribute NAME="Value" VALUE="2.4"></Attribute>
<Attribute NAME="collectionTime" VALUE="1525118288000"></Attribute>
<Attribute NAME="State" VALUE="normal"></Attribute>
<Attribute NAME="ObjectName" VALUE="obj1"></Attribute>
<Attribute NAME="ValueHistory" VALUE="5072"></Attribute>
</Target>
<Target TYPE="myserver.mgmt.Metric" NAME="metric999">
<Attribute NAME="name" VALUE="metric999"></Attribute>
<Attribute NAME="Value" VALUE="60.35"></Attribute>
<Attribute NAME="collectionTime" VALUE="1525118288000"></Attribute>
<Attribute NAME="State" VALUE="normal"></Attribute>
<Attribute NAME="ObjectName" VALUE="obj1"></Attribute>
<Attribute NAME="ValueHistory" VALUE="9550"></Attribute>
</Target>
</Targets>
')