我有一个如下所示的 XML 数据集:
<protocol ID='.'>
<HEAD></HEAD>
<block ID='...'>
<HEAD></HEAD>
<trial ID='.....'>
<HEAD></HEAD>
<seq ID=''>
<HEAD></HEAD>
<calibration CLASS='affine-calibration' ID='New Calibration'>
<AX>.........</AX>
<BX>-........</BX>
<AY>.........</AY>
<BY>.........</BY>
<type>'por'</type>
</calibration>
<POR TIME='......'>
<PUPIL>.</PUPIL>
<BLINK>.</BLINK>
<V>...</V>
<H>...</H>
<PLANEINTRWV>...</PLANEINTRWV>
<PLANEINTRWH>...</PLANEINTRWH>
<PLANE>.</PLANE>
</POR>
<POR TIME='......'>
<PUPIL>.</PUPIL>
<BLINK>.</BLINK>
<V>...</V>
<H>...</H>
<PLANEINTRWV>...</PLANEINTRWV>
<PLANEINTRWH>...</PLANEINTRWH>
<PLANE>.</PLANE>
</POR>
<POR TIME='......'>
<PUPIL>.</PUPIL>
<BLINK>.</BLINK>
<V>...</V>
<H>...</H>
<PLANEINTRWV>...</PLANEINTRWV>
<PLANEINTRWH>...</PLANEINTRWH>
<PLANE>.</PLANE>
</POR>
</seq>
</trial>
<trial ID='.....'>
<HEAD></HEAD>
<seq ID=''>
<HEAD></HEAD>
<calibration CLASS='affine-calibration' ID='New Calibration'>
<AX>.........</AX>
<BX>-........</BX>
<AY>.........</AY>
<BY>.........</BY>
<type>'por'</type>
</calibration>
<POR TIME='......'>
<PUPIL>.</PUPIL>
<BLINK>.</BLINK>
<V>...</V>
<H>...</H>
<PLANEINTRWV>...</PLANEINTRWV>
<PLANEINTRWH>...</PLANEINTRWH>
<PLANE>.</PLANE>
</POR>
<POR TIME='......'>
<PUPIL>.</PUPIL>
<BLINK>.</BLINK>
<V>...</V>
<H>...</H>
<PLANEINTRWV>...</PLANEINTRWV>
<PLANEINTRWH>...</PLANEINTRWH>
<PLANE>.</PLANE>
</POR>
</seq>
</trial>
</block>
</protocol>
使用 XML 包,提取 POR 标记的子标记和标记属性的最简洁方法是什么?
我把这个有用的东西拼凑在一起,但它很慢(由于很可能是 xpathSApply 调用)并且几乎不可读。
trackToDataFrame = function(file) {
doc2=xmlParse(file)
timeStamps = t(xpathSApply(doc2, '//*[@TIME]', function(x) c(name=xmlName(x), xmlAttrs(x))))
dd2 = xmlToDataFrame(getNodeSet(doc2, "//POR"), colClasses=c(rep("integer", 7)))
dd2 = cbind(dd2, timeStamps)
dd2
}
调用数据集返回:
PUPIL BLINK V H PLANEINTRWV PLANEINTRWH PLANE name TIME
1 NA NA NA NA NA NA NA POR ......
2 NA NA NA NA NA NA NA POR ......
3 NA NA NA NA NA NA NA POR ......
4 NA NA NA NA NA NA NA POR ......
5 NA NA NA NA NA NA NA POR ......
我认为整个事情可以通过一个 xmlToDataFrame 调用来完成,但我对 XML 包不够熟悉,无法让它工作。
我真正感兴趣的是“时间”列以及从 xmlToDataFrame 调用中提取的所有列。