With @SchaunW help, I was able to figure out How to parse XML to R data frame
But in my data, I need to parse more than one XML data, my code is as follow, the codes runs good for the first couple stations, but if run for the entire 500 stations, the error pop out:
"Error in temps.i[sapply(temps.i, function(x) any(unlist(x) == "hourly"))] :
invalid subscript type 'list'":
Please help, thanks!
data.all = data.frame()
lat = data.0$lat
lon = data.0$lon
head(data.0)
station_id LocID lat lon
10001 11694 32.82 -86.65
10079 089214 27.65 -80.23 (node 'temperature' not exit in XML)
data.loop <- lapply(1:length(data.0$station_id), function(i) {
urls.i <- paste("http://forecast.weather.gov/MapClick.php?lat=",lat[i],"&lon=",lon[i],"&FcstType=digitalDWML",sep="")
data.i <- xmlParse(urls.i)
xml_data.i <- xmlToList(data.i)
location.i <- as.list(xml_data.i[["data"]][["location"]][["point"]])
start_time.i <- unlist(xml_data.i[["data"]][["time-layout"]][names(xml_data.i[["data"]][["time-layout"]]) == "start-valid-time"])
temps.i <- xml_data.i[["data"]][["parameters"]]
temps.i <- temps.i[names(temps.i) == "temperature"]
temps.i <- temps.i[sapply(temps.i, function(x) any(unlist(x) == "hourly"))]
temps.i <- unlist(temps.i[[1]][sapply(temps.i, names) == "value"])
data1.i <- data.frame(as.list(location.i), "hh" = start_time.i, "Temp" = temps.i)
})
data.all <- as.data.frame(do.call(rbind, data.loop))