2016-03-30 143 views
0

我的示例XML中提取數據幀解析XML文件:從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> 

我想提取的「速度」,「代碼」和「cvalue」的所有條目時分數等於30。只有當得分爲30時,cvalue才存在,否則將被省略。目前我的代碼是

input <- xmlParse("20160128-235648.xml") 
nodes <- getNodeSet(input,"//Segment[@score='30']") 

這是創建包含數據只有當比分是30。它看起來像這樣

[[4584]] 
<Segment code="63365958" speed="41" score="30" c-value="0"> 
    <SubSegment speed="41" offset="0,433"/> 
</Segment> 

attr(,"class") 
[1] "XMLNodeSet" 

然而,XML節點集「節點」,我不能這樣XMLnodeset轉換爲一個數據幀的值爲「code」,「cvalue」,速度爲「lapply(nodes, function (x) xmlSApply(x,xmlValue))」是從「子段」而不是「段」提取空白向量

我還需要在「SegmentSpeedResults」中存儲「timestamp」單獨變量

+0

請參閱:http://stackoverflow.com/questions/36247451/r-xml-tree-to-dataframe/36247826#36247826。請注意,您將需要xmlAttrs而不是xmlValue。 –

+0

使用'sapply(節點,函數(x)xmlSApply(x,xmlAttrs))'從「子段」中提取數據幀而不是從「段」 – Sumit

回答

0

用xAttrs做補充解決了我在節點「Segment」下提取值的問題。這工作對我來說很好

nodes <- getNodeSet(input,"//Segment[@score='30']") 
all_parameters <- sapply(nodes, xmlAttrs)