解析对查询的 XML 响应 [英] Parsing an XML response to a query

查看:24
本文介绍了解析对查询的 XML 响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试提取大量经纬度线的县 fips 数.我可以从 FCC API 获取数据,但很难将其读入 R.

I am trying to extract the County fips number for a large number of lines of longitude and latitude. I can get the data from an FCC API, but am having a hard time reading it into R.

例如,当我在 R 中运行以下代码时:

For example, when I run the following code in R:

library(httr)
fips <- GET("http://data.fcc.gov/api/block/find", query = list(latitude = 48.9905, longitude = -122.2733, showall="false"))

result <- content(fips, as = "parsed")

result

对象结果"显示如下

{xml_document}
<Response>
[1] <Block FIPS="530730102002091"/>
[2] <County FIPS="53073" name="Whatcom"/>
[3] <State FIPS="53" code="WA" name="Washington"/>

我感兴趣的信息是县 FIPS 代码53073".我应该如何只提取那个数字?

The information I am interested in is the county FIPS code "53073." How should I go about extracting just that number?

推荐答案

您必须解析从该 API 返回的 XML

You have to parse the XML returned from that API

library("httr")
library("XML")
fips <- GET("http://data.fcc.gov/api/block/find", query = list(latitude = 48.9905, longitude = -122.2733, showall="false"))
result <- content(fips, as = "parsed")
> xmlToList(xmlParse(result))$County["FIPS"]
   FIPS 
"53073" 

这篇关于解析对查询的 XML 响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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