File: ecb.R

package info (click to toggle)
r-cran-xml 3.99-0.19-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,688 kB
  • sloc: ansic: 6,659; xml: 2,890; asm: 486; sh: 12; makefile: 2
file content (34 lines) | stat: -rw-r--r-- 1,185 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Reading European bank exchange rates.

history = "~/eurofxref-hist.xml"
daily = "~/eurofxref-daily.xml"
# f = "http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml"

f = history

d = xmlTreeParse(f, useInternalNodes = TRUE)
getNodeSet(d, "/gesmes:Envelope//Cube[@currency='SIT']", c(gesmes="http://www.gesmes.org/xml/2002-08-01"))
getNodeSet(d, "//gesmes:Envelope", c(gesmes="http://www.gesmes.org/xml/2002-08-01"))

namespaces = c(ns = "http://www.ecb.int/vocabulary/2002-08-01/eurofxref")



namespaces <- c(ns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref")


# Get the data for Slovenian currency for all time periods.
# Find all the nodes of the form <Cube currency="SIT"...>

slovenia = getNodeSet(d, "//ns:Cube[@currency='SIT']", namespaces )
# Now we have a list of such nodes, loop over them and get the rate  
# attribute

rates = as.numeric( sapply(slovenia, xmlGetAttr, "rate") )
# Now put the date on each element
# find nodes of the form <Cube time=".." ... >
# and extract the time attribute
names(rates) = sapply(getNodeSet(d, "//ns:Cube[@time]", namespaces ), 
                      xmlGetAttr, "time")

#  Or we could turn these into dates with strptime().