I want to extract exchange rates from the ECB website to convert my local currencies data. However, I am struggling a lot with using xpath (although this helped me a lot).
library(XML)
fileURL <- "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml?93aad09b8f8b7bdb69cd1574b5b2665f"
download.file(fileURL, destfile=tf <- tempfile(fileext=".xml"))
xml_file <- xmlParse(tf)
xml_data <- xmlRoot(xml_file)
currency <- xml_data[["number(//Cube/@currency)"]]
rate <- xml_data[["number(//Cube/@rate)"]]
Then I just want to create simple data frame:
df <- data.frame(currency, rate)
1) xpathSApply The following line gives a character matrix m
with currency and rate columns:
m <- t(xpathSApply(xml_data, "//*[@rate]", xmlAttrs))
If needed in the form of a data frame with character and numeric columns add this:
read.table(text = paste(m[, 1], m[, 2]), as.is = TRUE)
Note: We avoided having to deal with namespaces by using *
in the XPath expression but if it were desired to explicitly refer to Cube
, as in the question, then it would be done like this:
m <- xpathSApply(xml_data, "//x:Cube[@rate]", xmlAttrs, namespaces = "x")
2) read.pattern An alternative way is to parse the XML file using read.pattern
in gsubfn. (This does not use the XML package.)
library(gsubfn)
read.pattern(tf, pattern = "'(...)' rate='([0-9.]+)'", col.names = c("currency", "rate"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With