2014-06-10 2 views
3

나는에있는 웹에서 XML 읽어하려고에 대한 이해 XML :읽기 및 R

Error: XML content does not seem to be XML: 'https://d396qusza40orc.cloudfront.net/getdata%2Fdata%2Frestaurants.xml' 

내 코드 :

내가 R에 다음과 같은 오류를 얻고있다 https://d396qusza40orc.cloudfront.net/getdata%2Fdata%2Frestaurants.xml

install.packages("XML") 
library(XML) 
fileURL = "https://d396qusza40orc.cloudfront.net/getdata%2Fdata%2Frestaurants.xml" 
doc = xmlTreeParse(fileURL) 

XML 파일을 읽고 얼마나 많은 레스토랑이 우편 번호 21231을 갖고 있는지 알고 싶습니까?

감사 xml 파일을 다운로드

+0

그 함수의 설명서를 참조하십시오. http://cran.r-project.org/web/packages/XML/XML.pdf 'isUrl' 매개 변수를 적절히 채워야 할 것으로 생각됩니다. – hek2mgl

+0

이 작업을 시도했지만 작동하지 않았습니다 ... 오류 : 외부 엔티티 "https://d396qusza40orc.cloudfront.net/getdata%2Fdata%2Frestaurants.xml"을로드하지 못했습니다. 오류 : 1 : 외부 엔티티 "https ://d396qusza40orc.cloudfront.net/getdata%2Fdata%2Frestaurants.xml " – Shery

답변

6

시도 :

library(XML) 
fileURL <- "https://d396qusza40orc.cloudfront.net/getdata%2Fdata%2Frestaurants.xml" 
download.file(fileURL, destfile=tf <- tempfile(fileext=".xml")) 
doc <- xmlParse(tf) 
zip <- xpathSApply(doc, "/response/row/row/zipcode", xmlValue) 
sum(zip == "21231") 
# [1] 127 
+1

이유를 설명해 주시겠습니까? 설명서에 URL이 유효하다고 나와 있습니다. – hek2mgl

+2

유효하지만 https에 대한 인증서 확인에 실패했습니다. 'ssl.verifypeer = FALSE'를 기본''RCurl :: getURL' (?)에 전달할 수 있는지 모르겠습니다. 그러나'download.file' 또는'readLines' 또는'RCurl :: getURL (..., ssl.verifypeer = FALSE)'또는'http'에 의한'https' 교환도 가능합니다. – lukeA

+0

소리가 적당합니다. 불행히도 나는 atm을 테스트 할 수 없다. – hek2mgl