使用 r 按属性从 xml 获取值 [英] get value from xml with r by attribute

查看:34
本文介绍了使用 r 按属性从 xml 获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 xml 中获取如下所示的值:

I'm trying to get values from xml that looks like this:

<data>
    <result name="r">
        <item>
            <str name="id">123</str>
            <str name="xxx">aaa</str>
        </item>
        <item>
            <str name="id">456</str>
            <str name="xxx">aaa</str>
        </item>
    </result>
</data>

到目前为止,我可以通过以下方式获取id值:

So far, I can get the id value in the following way:

xmlfile <- xmlParse(url)
data <- xmlRoot(xmlfile) 
result <- xmltop[["result"]]
for (i in xmlSize(result)) {
  print(xmlValue(result[[i]][[1]]))
}

这似乎非常低效,并且仅当id"存储在第一个子元素中时才有效.那么,有没有办法通过搜索属性(name)和值(id)来获取元素的值(123, 456)>)?

This seems highly inefficient and only works if "id" is stored in the first child element. So, is there a way to get the value of an element (123, 456) by searching for the attribute (name) and value (id)?

推荐答案

xml2 包非常适合解决此类问题.

The xml2 package is very good for solving this type of problem.

library(xml2)
page<-read_xml('<data>
    <result name="r">
               <item>
               <str name="id">123</str>
               <str name="xxx">aaa</str>
               </item>
               <item>
               <str name="id">456</str>
               <str name="xxx">aaa</str>
               </item>
               </result>
               </data>')

#find all str nodes
 nodes<-xml_find_all(page, ".//str")
#filter out the nodes where the attribute name=id
 nodes<-nodes[xml_attr(nodes, "name")=="id"]
#get values (as character strings)
 xml_text(nodes)

这一切都可以在一行代码中完成,但为了清楚起见,这些步骤被分为三个.

This all could be done in one line of code, but the steps are broken into three for clarity.

这篇关于使用 r 按属性从 xml 获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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