我正在尋找一種有效的解決方案來提取 xml 查詢背后的所有時間序列。我的代碼是:
library(xml2)
# URL of the data provider
url.iscb <- "http://www.sedlabanki.is/xmltimeseries/"
# The data frame to store all the time series
iscb.rates <- data.frame()
# Dates defining the time range
d.all <- as.Date("1990-01-01")
d.now <- Sys.Date()
# XML
u <- paste0(url.iscb,"Default.aspx?DagsFra=",d.all,"T00:00:00&DagsTil=",
d.now,"T23:59:59&GroupID=1&Type=xml")
# Obtaining the data from the web site...
f <- xml2::read_xml(u)
doc <- xml2::as_list(f)
到目前為止,我無法提取f. 該變數doc似乎只存盤一個時間序列。
uj5u.com熱心網友回復:
嘗試這個:
library(xml2)
library(magrittr)
# URL of the data provider
url.iscb <- "http://www.sedlabanki.is/xmltimeseries/"
# Dates defining the time range
d.all <- as.Date("1990-01-01")
d.now <- Sys.Date()
# XML
u <- paste0(url.iscb,"Default.aspx?DagsFra=",d.all,"T00:00:00&DagsTil=",
d.now,"T23:59:59&GroupID=1&Type=xml")
# Obtaining the data from the web site...
f <- xml2::read_xml(u)
#Find the timeseries
timeseries <- xml_find_all(f, ".//TimeSeries")
timeseriesID <- timeseries %>% xml_attr("ID")
#timeseries %>% xml_find_all(".//Name") %>% xml_text()
#now step through each timeseries and extract the data
dfs <- lapply(1:length(timeseries), function(index){
currentNode <- timeseries[index]
#Find all of the Entry Nodes
entries <- xml_find_all(currentNode, ".//Entry")
#Extract the Date and Value from each node
dates <- xml_find_first(entries, ".//Date") %>% xml_text()
values <- xml_find_first(entries, ".//Value") %>% xml_double()
# The data frame to store all the time series
iscb.rates <- data.frame(timeseriesID[index], dates, values)
})
#dfs is a list of dataframes
#combine into 1 dataframe
dplyr::bind_rows(dfs)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/321721.html
