【问题标题】:How do I select a value in R from an xml document?如何从 xml 文档中选择 R 中的值?
【发布时间】:2021-10-30 06:19:49
【问题描述】:

我有一些健康数据的 xml 文档,这是默认的苹果健康数据库导出格式。我在这里截断了该文档,但它是一个有效的 xml 文档。如何选择与另一个属性匹配的所有“值”属性?例如,如何获得所有 type="HKQuantityTypeIdentifierBodyMass" 的矩阵或数据框?还是我所有的身高?

我尝试过类似的方法,但它只返回空值。

library(xml2)

xml <- read_xml('<?xml version="1.0" encoding="UTF-8"?>
<HealthData locale="en_US">
 <ExportDate value="2021-10-29 20:24:26 -0700"/>
 <Me HKCharacteristicTypeIdentifierDateOfBirth="1975-05-18" HKCharacteristicTypeIdentifierBiologicalSex="HKBiologicalSexMale" HKCharacteristicTypeIdentifierBloodType="HKBloodTypeNotSet" HKCharacteristicTypeIdentifierFitzpatrickSkinType="HKFitzpatrickSkinTypeNotSet" HKCharacteristicTypeIdentifierCardioFitnessMedicationsUse="None"/>
 <Record
         type="HKQuantityTypeIdentifierHeight"
         sourceName="Neil’s Apple Watch"
         sourceVersion="2.1"
         unit="ft"
         creationDate="2016-02-23 14:12:38 -0700"
         startDate="2016-02-23 14:12:38 -0700"
         endDate="2016-02-23 14:12:38 -0700"
         value="6.16667"/>
 <Record type="HKQuantityTypeIdentifierHeight" sourceName="Neil’s Apple Watch" sourceVersion="2.2.1" unit="ft" creationDate="2016-08-17 08:00:37 -0700" startDate="2016-08-17 08:00:37 -0700" endDate="2016-08-17 08:00:37 -0700" value="6.16667"/>
 <Record type="HKQuantityTypeIdentifierBodyMass" sourceName="Neil’s Apple Watch" sourceVersion="2.1" unit="lb" creationDate="2016-02-23 14:12:38 -0700" startDate="2016-02-23 14:12:38 -0700" endDate="2016-02-23 14:12:38 -0700" value="175"/>
 <Record type="HKQuantityTypeIdentifierBodyMass" sourceName="Neil’s Apple Watch" sourceVersion="2.2.1" unit="lb" creationDate="2016-08-17 08:00:36 -0700" startDate="2016-08-17 08:00:36 -0700" endDate="2016-08-17 08:00:36 -0700" value="180"/>
 <Record type="HKQuantityTypeIdentifierBodyMass"
         sourceName="Neils Apple Watch"
         sourceVersion="2.1"
         unit="lb"
         creationDate="2016-02-23 14:12:38 -0700"
         startDate="2016-02-23 14:12:38 -0700"
         endDate="2016-02-23 14:12:38 -0700"
         value="175"/>
</HealthData>')

listOfAllMyHeights <- xml_text(xml, "???")
listOfAllMyWeights <- xml_something("???") 


print(heights)```

【问题讨论】:

    标签: r xpath


    【解决方案1】:
    library(tidyverse)
    xml2::xml_find_all(xml, ".//Record") %>% 
      purrr::map(xml_attrs) %>%
      purrr::map_df(as.list)
      
    #   type                             sourceName  sourceVersion unit  creationDate  startDate  endDate  value
    #   <chr>                            <chr>       <chr>         <chr> <chr>         <chr>      <chr>    <chr>
    # 1 HKQuantityTypeIdentifierHeight   Neil’s App~ 2.1           ft    2016-02-23 1~ 2016-02-2~ 2016-02~ 6.16~
    # 2 HKQuantityTypeIdentifierHeight   Neil’s App~ 2.2.1         ft    2016-08-17 0~ 2016-08-1~ 2016-08~ 6.16~
    # 3 HKQuantityTypeIdentifierBodyMass Neil’s App~ 2.1           lb    2016-02-23 1~ 2016-02-2~ 2016-02~ 175  
    # 4 HKQuantityTypeIdentifierBodyMass Neil’s App~ 2.2.1         lb    2016-08-17 0~ 2016-08-1~ 2016-08~ 180  
    # 5 HKQuantityTypeIdentifierBodyMass Neils Appl~ 2.1           lb    2016-02-23 1~ 2016-02-2~ 2016-02~ 175  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 2012-04-10
      相关资源
      最近更新 更多