我正在努力抓取一个依赖 .atomsvc 文件的公共数据源,以允许用户在 Excel 中设置数据馈送。我使用 XML 库在 R 中构建了一个非常脆弱的解析器来提取 URL。我想知道如何在 xml2 中做到这一点(最好以更简洁和优雅的方式)
这是我使用 XML 库的方法
# Crystal Reports Parser Sample
library(XML)
library(dplyr)
# Get the .atomsvc file from the Export to Data Feed Option on the PA DEP website
pa_string <- '<?xml version="1.0" encoding="utf-8" standalone="yes"?><service xmlns:atom="http://www.w3.org/2005/Atom" xmlns:app="http://www.w3.org/2007/app" xmlns="http://www.w3.org/2007/app"><workspace><atom:title>Oil_Gas_Well_Production</atom:title><collection href="http://www.depreportingservices.state.pa.us/ReportServer?%2FOil_Gas%2FOil_Gas_Well_Production&P_PERIOD_ID=198&P_COUNTY%3Aisnull=True&P_CLIENT%3Aisnull=True&P_PERMIT_NUM%3Aisnull=True&P_OGO_NUM%3Aisnull=True&P_PRODUCING%3Aisnull=True&rs%3AParameterLanguage=&rs%3ACommand=Render&rs%3AFormat=ATOM&rc%3ADataFeed=xAx0x2"><atom:title>Tablix1</atom:title></collection></workspace></service>'
pa_list <- pa_string %>% xmlParse() %>% xmlToList()
# Extract the URL
URL <- URLdecode(pa_list$workspace$collection$.attrs)
据我所知,这是 xml2 版本
# Crystal Reports xml2 Parser
library(xml2)
library(dplyr)
# Get the .atomsvc file from the Export to Data Feed Option on the PA DEP website
pa_string <- '<service xmlns:atom="http://www.w3.org/2005/Atom" xmlns:app="http://www.w3.org/2007/app" xmlns="http://www.w3.org/2007/app"><workspace><atom:title>Oil_Gas_Well_Production</atom:title><collection href="http://www.depreportingservices.state.pa.us/ReportServer?%2FOil_Gas%2FOil_Gas_Well_Production&P_PERIOD_ID=198&P_COUNTY%3Aisnull=True&P_CLIENT%3Aisnull=True&P_PERMIT_NUM%3Aisnull=True&P_OGO_NUM%3Aisnull=True&P_PRODUCING%3Aisnull=True&rs%3AParameterLanguage=&rs%3ACommand=Render&rs%3AFormat=ATOM&rc%3ADataFeed=xAx0x2"><atom:title>Tablix1</atom:title></collection></workspace></service>'
pa_list <- pa_string %>% read_xml() %>% as_list()
我不知道如何从这里提取 URL,或者这是否是思考如何思考这个问题的正确方法。非常感激任何的帮助!