【问题标题】:xml2 extract URL from .atomsvc filexml2 从 .atomsvc​​ 文件中提取 URL
【发布时间】:2023-03-27 04:45:01
【问题描述】:

我正在抓取一个依赖 .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&amp;P_PERIOD_ID=198&amp;P_COUNTY%3Aisnull=True&amp;P_CLIENT%3Aisnull=True&amp;P_PERMIT_NUM%3Aisnull=True&amp;P_OGO_NUM%3Aisnull=True&amp;P_PRODUCING%3Aisnull=True&amp;rs%3AParameterLanguage=&amp;rs%3ACommand=Render&amp;rs%3AFormat=ATOM&amp;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&amp;P_PERIOD_ID=198&amp;P_COUNTY%3Aisnull=True&amp;P_CLIENT%3Aisnull=True&amp;P_PERMIT_NUM%3Aisnull=True&amp;P_OGO_NUM%3Aisnull=True&amp;P_PRODUCING%3Aisnull=True&amp;rs%3AParameterLanguage=&amp;rs%3ACommand=Render&amp;rs%3AFormat=ATOM&amp;rc%3ADataFeed=xAx0x2"><atom:title>Tablix1</atom:title></collection></workspace></service>'

pa_list <- pa_string %>% read_xml() %>% as_list()

我不知道如何从这里提取 URL,或者这是否是思考如何思考这个问题的正确方法。任何帮助将不胜感激!

【问题讨论】:

    标签: r xml2


    【解决方案1】:

    这是一种方法,通过从指定节点中提取属性:

    library(xml2)
    library(tidyverse)
    
    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&amp;P_PERIOD_ID=198&amp;P_COUNTY%3Aisnull=True&amp;P_CLIENT%3Aisnull=True&amp;P_PERMIT_NUM%3Aisnull=True&amp;P_OGO_NUM%3Aisnull=True&amp;P_PRODUCING%3Aisnull=True&amp;rs%3AParameterLanguage=&amp;rs%3ACommand=Render&amp;rs%3AFormat=ATOM&amp;rc%3ADataFeed=xAx0x2"><atom:title>Tablix1</atom:title></collection></workspace></service>'
    
     pa_string %>% 
      read_xml() %>% 
      xml_find_all("//*[name()='collection']")%>%
      xml_attr("href") 
    #output
    [1] "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"
    
    xpath:
    
    #// - Recursive descent; searches for the specified element at any depth. 
    #* -  Matches any element node
    #[ ] - Applies a filter pattern.
    #name()='collection' - self explanatory
    

    更短:

    pa_string %>% 
      read_xml() %>% 
      xml_find_all("//@href") #select all attributes with name `href`
    

    由于只有一个元素具有属性,所以也可以这样做:

    pa_string %>% 
      read_xml() %>% 
      xml_find_all("//@*") #Matches any attribute node
    

    【讨论】:

    • 效果很好。正确使用 xml_find_all 对我来说是新的。非常感谢!
    • 感谢您的编辑。添加了对 xpath 的说明。解析大型 xml 真的很强大。 w3schools.com/xml/xpath_syntax.asp
    猜你喜欢
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 2019-01-07
    • 2015-03-19
    • 2020-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多