如何parsingXML到R数据框

我试图parsingXML到R数据框,这个链接帮了我很多:

如何从xml文件创build一个R数据框

但是我还是无法弄清我的问题:

这是我的代码:

data <- xmlParse("http://forecast.weather.gov/MapClick.php?lat=29.803&lon=-82.411&FcstType=digitalDWML") xmlToDataFrame(nodes=getNodeSet(data1,"//data"))[c("location","time-layout")] step1 <- xmlToDataFrame(nodes=getNodeSet(data1,"//location/point"))[c("latitude","longitude")] step2 <- xmlToDataFrame(nodes=getNodeSet(data1,"//time-layout/start-valid-time")) step3 <- xmlToDataFrame(nodes=getNodeSet(data1,"//parameters/temperature"))[c("type="hourly"")] 

我想要的数据框是这样的:

 latitude longitude start-valid-time hourly_temperature 29.803 -82.411 2013-06-19T15:00:00-04:00 91 29.803 -82.411 2013-06-19T16:00:00-04:00 90 

我卡在xmlToDataFrame,任何帮助将非常感激,谢谢。

XML格式的数据很less以允许xmlToDataFrame函数工作的方式组织。 您最好提取列表中的所有内容,然后在数据框中将列表绑定在一起:

 require(XML) data <- xmlParse("http://forecast.weather.gov/MapClick.php?lat=29.803&lon=-82.411&FcstType=digitalDWML") xml_data <- xmlToList(data) 

在您的示例数据的情况下,获取位置和开始时间相当简单:

 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"]) 

温度数据有点复杂。 首先你需要到达包含温度列表的节点。 然后,您需要提取两个列表,在每个列表中查找,然后select“小时”作为其值之一。 然后,您只需要select该列表,但只保留具有“值”标签的值:

 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 

更直接地使用xpath来提高性能和清晰度。

 time_path <- "//start-valid-time" temp_path <- "//temperature[@type='hourly']/value" df <- data.frame( latitude=data[["number(//point/@latitude)"]], longitude=data[["number(//point/@longitude)"]], start_valid_time=sapply(data[time_path], xmlValue), hourly_temperature=as.integer(sapply(data[temp_path], as, "integer")) 

导致

 > head(df, 2) latitude longitude start_valid_time hourly_temperature 1 29.81 -82.42 2014-02-14T18:00:00-05:00 60 2 29.81 -82.42 2014-02-14T19:00:00-05:00 55 

这是一个使用xml2的部分解决scheme。 将解决scheme分解成小块通常可以更容易地确保一切排列:

 library(xml2) data <- read_xml("http://forecast.weather.gov/MapClick.php?lat=29.803&lon=-82.411&FcstType=digitalDWML") # Point locations point <- data %>% xml_find_all("//point") point %>% xml_attr("latitude") %>% as.numeric() point %>% xml_attr("longitude") %>% as.numeric() # Start time data %>% xml_find_all("//start-valid-time") %>% xml_text() # Temperature data %>% xml_find_all("//temperature[@type='hourly']/value") %>% xml_text() %>% as.integer() 

你可以尝试下面的代码:

 # Load the packages required to read XML files. library("XML") library("methods") # Convert the input xml file to a data frame. xmldataframe <- xmlToDataFrame("input.xml") print(xmldataframe)