get value from xml with r by attribute

Viewed 3869

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>

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]]))
}

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)?

1 Answers
Related