【发布时间】:2016-03-30 23:36:18
【问题描述】:
我的示例 XML
- <Inrix responseId="123" statusText="" createdDate="2016-01-29T05:57:00Z">
- <SegmentSpeedResultSet coverage="255">
- <SegmentSpeedResults timestamp="2016-01-29T05:56:47Z">
- <score="10" speed="57" code="4814018">
<SubSegment speed="57" offset="0,1753"/>
</Segment>
- <score="30" speed="57" code="4814018" cvalue="57">
<SubSegment speed="57" offset="0,1753"/>
</Segment>
</SegmentSpeedResults>
</SegmentSpeedResultSet>
</Inrix>
我想在分数等于 30 时提取所有条目的“速度”、“代码”和“cvalue”。cvalue 仅在分数为 30 时才存在,否则将被省略。目前我的代码是
input <- xmlParse("20160128-235648.xml")
nodes <- getNodeSet(input,"//Segment[@score='30']")
这是创建包含数据的 XML 节点集“节点”,仅当分数为 30 时。看起来像这样
[[4584]]
<Segment code="63365958" speed="41" score="30" c-value="0">
<SubSegment speed="41" offset="0,433"/>
</Segment>
attr(,"class")
[1] "XMLNodeSet"
但是,我无法将此 XMLnodeset 转换为值为“code”、“cvalue”、speed”的数据帧。lapply(nodes, function (x) xmlSApply(x,xmlValue)) 正在从“Subsegment”而不是“Segment”中提取空白向量。
我还需要将“时间戳”作为单独的变量存储在“SegmentSpeedResults”中。
【问题讨论】:
-
见:stackoverflow.com/questions/36247451/r-xml-tree-to-dataframe/…。请注意,您将需要 xmlAttrs 而不是 xmlValue。
-
使用
sapply(nodes, function (x) xmlSApply(x,xmlAttrs))从“子段”而不是“段”中提取数据帧
标签: r xml-parsing xmlnode