XML data is rarely organized so that the xmlToDataFrame function xmlToDataFrame . You better extract everything in the lists and then link the lists together in a data frame:
require(XML) data <- xmlParse("http://forecast.weather.gov/MapClick.php?lat=29.803&lon=-82.411&FcstType=digitalDWML") xml_data <- xmlToList(data)
In the case of your example data, getting the location and startup time is pretty simple:
location <- as.list(xml_data[["data"]][["location"]][["point"]]) start_time <- unlist(xml_data[["data"]][["time-layout"]][ names(xml_data[["data"]][["time-layout"]]) == "start-valid-time"])
The temperature data is a little more complicated. First you need to get to node, which contains temperature lists. Then you need to extract both lists, look at each of them, and select the one that has an βhourlyβ value as one of its values. Then you need to select only this list, but only save the values ββlabeled "value":
temps <- xml_data[["data"]][["parameters"]] temps <- temps[names(temps) == "temperature"] temps <- temps[sapply(temps, function(x) any(unlist(x) == "hourly"))] temps <- unlist(temps[[1]][sapply(temps, names) == "value"]) out <- data.frame( as.list(location), "start_valid_time" = start_time, "hourly_temperature" = temps) head(out) latitude longitude start_valid_time hourly_temperature 1 29.81 -82.42 2013-06-19T16:00:00-04:00 91 2 29.81 -82.42 2013-06-19T17:00:00-04:00 90 3 29.81 -82.42 2013-06-19T18:00:00-04:00 89 4 29.81 -82.42 2013-06-19T19:00:00-04:00 85 5 29.81 -82.42 2013-06-19T20:00:00-04:00 83 6 29.81 -82.42 2013-06-19T21:00:00-04:00 80
SchaunW Jun 19 '13 at 19:57 2013-06-19 19:57
source share