I have XML with a bunch of envelope
elements. Inside of each one is an array. Each row in the array has 2 elements. The first is an identifier and the second is the text I want to grab. I need the first value of the row to identify the correct row so that I can grab the correct value.
In the example below I have 'food' in rows denoted with the code 610954
. I want to grab the 2 elements after this code (c('pizza', 'burger')
. Likewise there are 'drinks' denoted by the code 605380
. I want to grab c('coke', 'pepsi')
. How can I use the xml2 package to do this?
library(xml2)
library(magrittr)
myxml <- read_xml('
<inside>
<envelope>
<card-entries type="array">
<card-entry>
<card-id type="integer">605380</card-id>
<value>coke</value>
</card-entry>
<card-entry>
<card-id type="integer">610954</card-id>
<value>pizza</value>
</card-entry>
</card-entries>
</envelope>
<envelope>
<card-entries type="array">
<card-entry>
<card-id type="integer">605380</card-id>
<value>pepsi</value>
</card-entry>
<card-entry>
<card-id type="integer">610954</card-id>
<value>burger</value>
</card-entry>
</card-entries>
</envelope>
</inside>
'
)
## as far as I can parse it (but not specific enough)
myxml %>%
xml_find_all('//envelope/card-entries[@type="array"]/card-entry') %>%
xml_text()
food <- -CODE THAT GIVES HERE c('pizza', 'burger')- # 610954
drinks <- -CODE THAT GIVES HERE c('coke', 'pepsi')- # 605380
Your original approach could be modified like this to get the drinks:
But you could go with a variety of other approaches