Home > front end >  How to get data from an XML webpage
How to get data from an XML webpage

Time:11-13

I am interested in the data provided by the link: https://stats.bis.org/api/v1/data/WS_XRU_D/D.RU../all?detail=full

My code for retrieving the data on the daily exchange rate history so far has developed into just this basic lines, while it seems I am stuck with realization that I am not able to extract the core daily data I am interested in:

u <- "https://stats.bis.org/api/v1/data/WS_XRU_D/D.RU../all?detail=full"
d <- xml2::read_xml(u)
d

{xml_document}
<StructureSpecificData 
xmlns:ss="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/data/structurespecific
...
[1] <message:Header>\n  <message:ID>IDREF85e8b4cf-d7d2-4506-81e6-adf668cd841b</message:ID>\n  <message:Test>fa ...
[2] <message:DataSet ss:dataScope="DataStructure" xsi:type="ns1:DataSetType" ss:structureRef="BIS_WS_XRU_D_1_0 ...

I will appreciate very much for any suggestion on how to proceed correctly with xml data retreiving!

CodePudding user response:

You started on the correct track, it is just a matter of extracting the correct nodes and obtaining the attribute values:

library(xml2)
library(dplyr)

#read the page
url <- "https://stats.bis.org/api/v1/data/WS_XRU_D/D.RU../all?detail=full"
page <- xml2::read_xml(url)

#extract the OBS nodes
#<Obs TIME_PERIOD="1992-07-01" OBS_VALUE="0.12526" OBS_STATUS="A" OBS_CONF="F"/>
observations <- xml_find_all(page, ".//Obs")

#extract the attribute vales from each node
date <- observations %>% xml_attr("TIME_PERIOD")  %>% as.Date("%Y-%m-%d")
value <- observations %>% xml_attr("OBS_VALUE") %>% as.numeric()
status <- observations %>% xml_attr("OBS_STATUS")
conf <- observations %>% xml_attr("OBS_CONF")

answer <- data.frame(date, value, status, conf)
plot(answer$date, answer$value)
  • Related