繁体   English   中英

将XML解析为R中的data.frame

[英]Parsing XML to data.frame in R

对此有很多疑问,但找不到适合这种数据格式的解决方案。 感谢有关如何解析此问题的建议:

<XML>
<constituency hansard_id="5" id="uk.org.publicwhip/cons/1" fromdate="1918" todate="9999-12-31">
    <name text="Aberavon"/>
</constituency>
<constituency hansard_id="6" id="uk.org.publicwhip/cons/2" fromdate="1997-05-01" todate="2005-05-04">
    <name text="Aberdeen Central"/>
</constituency>
<constituency hansard_id="7" id="uk.org.publicwhip/cons/3" fromdate="1885" todate="9999-12-31">
    <name text="Aberdeen North"/>
</constituency>
</XML>

所需的字段显然是c('hansard_id','id','fromdate','todate','name') 读入和解析我尝试了以下内容:

require(XML)
> indata = htmlParse('data.xml', isHTML=F)
> class(indata)
[1] "XMLInternalDocument" "XMLAbstractDocument"
> print(indata)
<?xml version="1.0"?>
<XML>
  <constituency hansard_id="5" id="uk.org.publicwhip/cons/1" fromdate="1918" todate="9999-12-31">
    <name text="Aberavon"/>
  </constituency>
  <constituency hansard_id="6" id="uk.org.publicwhip/cons/2" fromdate="1997-05-01" todate="2005-05-04">
    <name text="Aberdeen Central"/>
  </constituency>
  <constituency hansard_id="7" id="uk.org.publicwhip/cons/3" fromdate="1885" todate="9999-12-31">
    <name text="Aberdeen North"/>
  </constituency>
</XML>

> xmlToDataFrame(indata, stringsAsFactors=F)
  name
1     
2     
3     

它正在读取,但xmlToDataFrame无法处理格式。 是因为数据是'constituency'标签元素的属性吗? 非常感谢任何指导。

你是对的, xmlToDataFrame只访问XML节点。 对于给定节点, xmlAttrs函数将返回该节点属性。 xpathApply函数接受解析的xml文档doc say并将xpath应用于它以获取一组节点。 然后将这些节点中的每一个应用于用户定义的功能。 xpath "//*/constituency"将返回文档中的所有constituency节点。 然后我们可以将xmlAttrs函数应用于每个:

res <- xpathApply(doc, "//*/constituency", xmlAttrs)

这将返回一个属性列表。 我们想将这些绑定在一起,例如:

rbind.data.frame(res[[1]], res[[2]], ...)

将第一个和第二个,第三个,......组的属性绑定到data.frame中。 这样做的一个简单方法是在out属性列表中使用do.call函数:

do.call(rbind.data.frame, res)

将行绑定应用于列表的所有元素。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM