【问题标题】:Extract nested data from XML as data frame in R从 XML 中提取嵌套数据作为 R 中的数据框
【发布时间】:2016-05-08 09:14:53
【问题描述】:

我正在尝试从 XML 文件中提取数据,示例结构如下:

<pwx creator="PerfPRO" version="1.0">
<workout>
<athlete></athlete>
<title></title>
<sportType>Bike</sportType>
<cmt></cmt>
<device id=""></device>
<time>2016-01-19T08:01:00</time>
<summarydata>
    <beginning>0</beginning>
    <duration>3600.012</duration>
</summarydata>
<segment>
    <summarydata>
        <beginning>0</beginning>
        <duration>120</duration>
    </summarydata>
</segment>
<segment>
    <summarydata>
        <beginning>120</beginning>
        <duration>120</duration>
    </summarydata>
</segment>
<segment>
    <summarydata>
        <beginning>240</beginning>
        <duration>120</duration>
    </summarydata>
</segment>

理想情况下,我想以数据框的形式访问“段”块(包括开始和持续时间)中的数据。有许多段块。

我尝试了很多东西,但似乎仍然无法提取它,我得到的只是一个空列表。这是我所做的(pwx 是文件名):

xmlData <- xmlInternalTreeParse(pwx, useInternalNodes = TRUE)
xmltop = xmlRoot(XMLdata)

d <- xpathSApply(doc = xmlData, path = "//pwx/workout/segment/summarydata/beginning", fun = xmlValue)

我似乎也可以通过以下方式访问所有细分:

segment <- xmltop[[1]]["segment"]

但似乎无法获取值。我在上面尝试了很多变化。

非常感谢任何帮助,谢谢。

编辑:

> summary(xmlData)
$nameCounts

    cad        dist          hr         pwr      sample         spd  timeoffset   beginning 
   3274        3274        3274        3274        3274        3274        3274          16 


duration summarydata     segment     athlete         cmt      device        make       model 
         16          16          15           1           1           1           1           1 
       name         pwx   sportType        time       title     workout 
          1           1           1           1           1           1 

$numNodes
[1] 22992

【问题讨论】:

    标签: xml r xml-parsing xmlnodelist


    【解决方案1】:

    这里是一些原始的xml2 处理,加上一点purrr:

    library(xml2)
    library(purrr)
    
    nodes <- xml_find_all(doc, ".//segment/summarydata")
    
    map_df(nodes, function(x) {
      kids <- xml_children(x)
      setNames(as.list(type.convert(xml_text(kids))), xml_name(kids))
    })
    
    ## Source: local data frame [3 x 2]
    ## 
    ##   beginning duration
    ##       (int)    (int)
    ## 1         0      120
    ## 2       120      120
    ## 3       240      120
    

    【讨论】:

      【解决方案2】:

      您应该查看rvest。以下可能不是最优雅的使用方式,但它确实有效。

      some_xml <- paste0(your_xml,'</workout></pwx>') # your example plus end of data to complete
      
      library('rvest')
      some_xml %>% read_xml %>% xml_nodes('summarydata')  -> nodes
      nodes %>% xml_nodes('beginning') %>% xml_text -> beginning
      nodes %>% xml_nodes('duration') %>% xml_text -> duration
      data.frame(beginning, duration, stringsAsFactors = FALSE)
      #   beginning duration
      # 1         0 3600.012
      # 2         0      120
      # 3       120      120
      # 4       240      120
      

      【讨论】:

      • 我在原始问题中添加了结尾行,仅供参考。
      【解决方案3】:

      感谢所有回复并提供答案的人。我无法像上面那样得到建议的答案(可能是我自己的失败)。

      为了完整性和参考,我设法让它工作:

      pwx <- "myfile.pwx"
      xmlData <- xmlInternalTreeParse(pwx, useInternalNodes = TRUE)
      xmltop = xmlRoot(xmlData)
      nodes <- getNodeSet(xmltop, '//as:summarydata', namespaces = c(as=xmlNamespace(xmltop)))
      df <-xmlToDataFrame(nodes)
      

      输出:

           beginning duration
      1          0 3600.012
      2          0      120
      3        120      120
      4        240      120
      5        360      120
      6        480      600
      7       1080      180
      8       1260      300
      9       1560      300
      10      1860      180
      11      2040      300
      12      2340      300
      13      2640      180
      14      2820      300
      15      3120      300
      16      3420  180.015
      

      谢谢,

      M

      【讨论】:

        猜你喜欢
        • 2020-07-14
        • 2020-07-30
        • 1970-01-01
        • 2021-06-17
        • 1970-01-01
        • 2023-03-29
        • 2017-12-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多