在 R(XML 包)中提取 xml 节点的第二个属性 [英] Extract second attribute of a xml node in R (XML package)

查看:27
本文介绍了在 R(XML 包)中提取 xml 节点的第二个属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从这样的 .xml 文件中提取lat"和long":

I want to extract both 'lat' and 'long' from a .xml file like this:

<asdf>
<dataset>
    <px lon="-55.75" lat="-18.5">2.186213</px>
    <px lon="-50.0"  lat="-18.5">0.0</px>
    <px lon="-66.75" lat="-03.0">1.68412</px>
    </dataset>
</asdf>

这是我到目前为止所做的,使用 R::XML 包:

this is what I've done so far, using the R::XML package:

#Load library for xml loading reading extracting
library(XML)

#Parse xml file 
a3  <- xmlRoot(xmlTreeParse("my_file.xml"))

#Extract text-value and attributes as lists
precip <- xmlSApply(a3, function(x) xmlSApply(x, xmlValue))
long   <- xmlSApply(a3, function(x) xmlSApply(x, xmlAttrs))
lat    <- xmlSApply(a3, function(x) xmlSApply(x, xmlAttrs)) #???

dt.lat.long.val <- data.frame(as.numeric(as.vector(lat)), 
                          as.numeric(as.vector(long)), 
                          as.numeric(as.vector(precip)))

如何编辑以 #??? 结尾的行以获得纬度值?

How do I edit the line ending in #??? so to get the lat values?

推荐答案

您可以使用以下方法提取数据

You can extract the data using something along these lines

test <- '<asdf>
<dataset>
    <px lon="-55.75" lat="-18.5">2.186213</px>
    <px lon="-50.0"  lat="-18.5">0.0</px>
    <px lon="-66.75" lat="-03.0">1.68412</px>
    </dataset>
</asdf>'

library(XML)
a3 <- xmlParse(test)

out <- xpathApply(a3, "//px", function(x){
  coords <- xmlAttrs(x)
  data.frame(precip = xmlValue(x), lon = coords[1], lat = coords[2], stringsAsFactors = FALSE)
})

> do.call(rbind.data.frame, out)
       precip    lon   lat
lon  2.186213 -55.75 -18.5
lon1      0.0  -50.0 -18.5
lon2  1.68412 -66.75 -03.0

这篇关于在 R(XML 包)中提取 xml 节点的第二个属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆