如果我有R中加載XML,例如:解析不規則的XML中的R
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
側問題/評論/警告 - 這些線路兩者碰撞RStudio: '限定符< - xpathApply(頂部, 「//限定符/ @ y」)的'' 限定符< - xpathApply(xmlRoot(top),「// qualifier/@ y」) 我不知道爲什麼。 – JPMac