【问题标题】:Can't Extract Data from XML using R无法使用 R 从 XML 中提取数据
【发布时间】:2014-02-19 20:36:42
【问题描述】:

所以我知道这里已经广泛讨论了这个话题。我在同一件事上发现了很多问题,但仍然无法弄清楚如何解析这个 XML 文件。我正在使用 R,我想从文件中提取经度和纬度。

I'm using this datathis guide 但似乎无法使其工作。

这是我的工作:

require(XML)  
data <- xmlParse("http://www.donatingplasma.org/index.php?option=com_storelocator&format=feed&searchall=1&Itemid=166&catid=-1&tagid=-1&featstate=0")
xml_data <- xmlToList(data)

一切正常。 XML 文件现在是一个“大列表”。当我尝试提取纬度和经度时,我迷路了。我试过了:

location <- as.list(xml_data[["marker"]][["lat"]])

并得到一个包含 1 行的列表。

我将如何从这个 XML 数据中提取纬度和经度?

数据结构示例:

<markers>
<limited>0</limited>
<marker>
<name>ADMA BioCenters</name>
<category>IQPP Certified</category>
<markertype>
/media/com_storelocator/markers/100713214004000000jl_marker2.png
</markertype>
<featured>false</featured>
<address>
6290 Jimmy Carter Boulevard, Suite 208, Norcross, Georgia 30071
</address>
<lat>33.9290629</lat>
<lng>-84.2204952</lng>
<distance>0</distance>
<fulladdress>
<![CDATA[
<p><img style="margin-left: auto; margin-right: auto;" src="images/jl_marker2.png" alt="jl marker2" width="22" height="22" />IQPP Certified</p>
]]>
</fulladdress>
<phone>678-495-5800</phone>
<url>http://www.atlantaplasma.com</url>
<email/>
<facebook/>
<twitter/>
<tags>
<![CDATA[ ]]>
</tags>
<custom1 name="Custom Field 1">
<![CDATA[ ]]>
</custom1>
<custom2 name="Custom Field 2">
<![CDATA[ ]]>
</custom2>
<custom3 name="Custom Field 3">
<![CDATA[ ]]>
</custom3>
<custom4 name="Custom Field 4">
<![CDATA[ ]]>
</custom4>
<custom5 name="Custom Field 5">
<![CDATA[ ]]>
</custom5>

【问题讨论】:

    标签: xml r parsing data-structures


    【解决方案1】:

    在原始 XML 上使用 xpathSapply 而不是遍历列表。

    lat <- xpathSApply(data, '//marker/lat', xmlValue)
    long <- xpathSApply(data, '//marker/lng', xmlValue)
    

    结果:

    > head(cbind(lat, long))
         lat          long         
    [1,] "33.9290629" "-84.2204952"
    [2,] "48.3097292" "14.299297"  
    [3,] "41.6134569" "-87.514584" 
    [4,] "41.5878273" "-87.3369907"
    [5,] "39.98504"   "-83.004705" 
    [6,] "43.2056277" "-86.2708023"
    

    根据@Martin Morgan 的评论,我认为在这里对不同的策略进行基准测试会很好:

    > microbenchmark(xpathSApply(data, '//marker/lat', xmlValue),
                     sapply(data["//marker/lat"], xmlValue),
                     sapply(data["//marker/lat"], as, "numeric"))
    Unit: milliseconds
                                            expr       min        lq   median       uq      max neval
     xpathSApply(data, "//marker/lat", xmlValue)  67.03714  97.57796 100.1633 102.1815 213.3031   100
          sapply(data["//marker/lat"], xmlValue)  72.73847 103.63095 106.1037 108.2251 132.6314   100
     sapply(data["//marker/lat"], as, "numeric") 257.16364 346.13708 389.3025 394.3669 598.3736   100
    

    好像

    显然,最后一个策略效率最低(这是有道理的,因为它在每个节点上调用类型转换。但这使得它不是一个完全公平的测试,因为最后一个表达式产生数字输出,而前两个产生字符输出。因此第二次测试:

    > microbenchmark(as.numeric(xpathSApply(data, '//marker/lat', xmlValue)), 
                     as.numeric(sapply(data["//marker/lat"], xmlValue)), 
                     sapply(data["//marker/lat"], as, "numeric"))
    Unit: milliseconds
                                                        expr       min        lq    median       uq      max neval
     as.numeric(xpathSApply(data, "//marker/lat", xmlValue))  60.29744  80.08186  97.94924 100.9548 189.0797   100
          as.numeric(sapply(data["//marker/lat"], xmlValue))  59.45891  85.47169 103.68015 106.5882 124.5708   100
                 sapply(data["//marker/lat"], as, "numeric") 210.92816 339.54831 384.28481 392.0001 481.4498   100
    

    同样,使用 xpathSApplysapply(使用 xpath 提取)会产生非常相似的结果。所以马丁第一个解决方案的修改版本:

    lat <- as.numeric(sapply(data["//marker/lat"], xmlValue))
    

    可能是这里最好的策略。

    【讨论】:

    • @RossWardrup 在 R 中的 XML 是一个真正的痛苦,除非你使用 xpath,当它突然 - 可能神奇地 - 变得容易得多。
    • 也许更简洁一点,使用 xpath 可以子集数据对象 sapply(data["//marker/lat"], xmlValue) 的概念或使用强制形式 as(x, "numeric") sapply(data["//marker/lat"], as, "numeric")
    猜你喜欢
    • 1970-01-01
    • 2013-04-06
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-23
    • 2020-02-22
    相关资源
    最近更新 更多