I am trying to parse XML returned by API as below:
library(httr2)
library(xml2)
library(tidyverse)
resp_xml <- request("https://data-api.ecb.europa.eu/service/data/CBD2/A..W0.11._Z._Z.A.A.A0000._X.ALL.CA._Z.LE._T.EUR") %>%
req_perform() %>%
resp_body_xml()
xml2::xml_find_all(resp_xml, "//generic:SeriesKey") %>%
xml2::xml_find_all("//generic:Value")
How can I extract only the values for id="REF_ARA"? I want to get the list just with country codes like "AT", "BE" etc.. I've tried something like this but I get only NAs:
xml2::xml_find_all(resp_xml, "//generic:SeriesKey") %>%
xml2::xml_find_all("//generic:Value") %>%
xml2::xml_attr("REF_AREA")
If you're trying to get the values of
value
for nodes whereid="REF_AREA"
, you can do so using XPath:Basically, you are trying to find nodes where the value of
id
(i.e. an attribute) is equal to"REF_AREA"
. The code below could help you to better understand this concept:Created on 2023-12-06 with reprex v2.0.2