在 R 中阅读和理解 XML [英] Reading and understanding XML in R

查看:31
本文介绍了在 R 中阅读和理解 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从位于以下位置的 Web 中读取 XML:https://d396qusza40orc.cloudfront.net/getdata%2Fdata%2Frestaurants.xml

I am trying to read in an XML from the web located at: https://d396qusza40orc.cloudfront.net/getdata%2Fdata%2Frestaurants.xml

我在 R 中遇到以下错误:

I am getting the following error in R:

Error: XML content does not seem to be XML: '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 的餐馆有多少?

I want to read that XML file and find out how many restaurants have zipcode 21231?

谢谢

推荐答案

尝试下载 xml 文件:

Try downloading the xml file:

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

这篇关于在 R 中阅读和理解 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆