如果我在 R 中加载了 xml,例如:
library(XML)
top <- newXMLNode("top")
tvp <- newXMLNode("TVP", parent = top)
time <- newXMLNode("time", "2012-01-01", parent = tvp)
value <- newXMLNode("value", "123", parent = tvp)
comment <- newXMLNode("comment",parent = tvp)
qualifer <-newXMLNode("qualifier", attrs = c(y = 'abc'), parent = comment)
commentText <-newXMLNode("info", attrs = c(y = 'something'), parent = comment)
tvp <- newXMLNode("TVP", parent = top)
time <- newXMLNode("time", "2012-01-02", parent = tvp)
value <- newXMLNode("value", "456", parent = tvp)
tvp <- newXMLNode("TVP", parent = top)
time <- newXMLNode("time", "2012-01-03", parent = tvp)
value <- newXMLNode("value", "789", parent = tvp)
comment <- newXMLNode("comment",parent = tvp)
newXMLNode("qualifier", attrs = c(y = 'efg'), parent = comment)
top
生成的 XML:
<top>
<TVP>
<time>2012-01-01</time>
<value>123</value>
<comment>
<qualifier y="abc"/>
<info y="something"/>
</comment>
</TVP>
<TVP>
<time>2012-01-02</time>
<value>456</value>
</TVP>
<TVP>
<time>2012-01-03</time>
<value>789</value>
<comment>
<qualifier y="efg"/>
</comment>
</TVP>
</top>
如何获得正确(即在正确的位置)包含限定符和信息属性的数据框?
这几乎可以工作,但不完全是:
DF <- xmlToDataFrame(top,stringsAsFactors=FALSE)
结果如下所示:
time value comment
1 2012-01-01 123
2 2012-01-02 456 <NA>
3 2012-01-03 789
我怎样才能得到:
我真正需要的是一种获取评论子节点属性的方法:
time value qualifer info
1 2012-01-01 123 abc something
2 2012-01-02 456
3 2012-01-03 789 efg