【问题标题】:Empty dataframe returned from xml data using xmlToDataframe in R使用 R 中的 xmlToDataframe 从 xml 数据返回的空数据框
【发布时间】:2021-05-08 09:37:50
【问题描述】:

处理使用 GET 函数从 httr 包下载的 xml 数据。返回的内容是 application/xml 类型。摘录如下:

<?xml version="1.0" encoding="UTF-8"?>
<metadata xmlns="http://example.com/schema/dxf/2.0">
  <pager>
    <page>1</page>
    <pageCount>17</pageCount>
    <total>819</total>
    <pageSize>50</pageSize>
    <nextPage>https://xxx.org.ng/xxx/api/indicators?page=2&amp;format=xml</nextPage>
  </pager>
  <indicators>
    <indicator id="cfvVUWwkwje">
      <displayName> ART Total </displayName>
    </indicator>
    <indicator id="gytvOB3J7">
      <displayName> ART Microscopy - Total</displayName>
    </indicator>
    <indicator id="5fgtZdtvQRW">
      <displayName> ART Microscopy Biology - Total</displayName>
    </indicator>
    <indicator id="g6hYenEHnsu">
      <displayName> ART GeneXpert - Total </displayName>
    </indicator>
    <indicator id="hhjxxDlG87j">
      <displayName> ART Functional -Total</displayName>
    </indicator>
    <indicator id="SarCtUBpBru">
      <displayName> ART 21 - Total</displayName>
    </indicator>
    <indicator id="ftywhPKoMgp">
      <displayName> Buruli Ulcer Total</displayName>
    </indicator>
    <indicator id="gyyhtAzCQZ0">
      <displayName> xART 21 prophylaxis Functional -Total</displayName>
    </indicator>
    <indicator id="vftWafaROyq0">
      <displayName> xART 21 Non Functional - Total</displayName>
    </indicator>
    </indicators>
</metadata>

我使用以下代码下载并尝试将xml转换为dataframe,如下所示:

url_xml <- modify_url(url1, path = path)
xml_response <- GET(url_xml, authenticate(username, password))

http_type(xml_response)
resp_content <- content(xml_response)

parsed_content <- xmlParse(resp_content)

# get the root
parsed_xml_root <- xmlRoot(parsed_content)
# parse out names and IDs
df_xml <- xmlToDataFrame(nodes = getNodeSet(parsed_xml_root,"//indicators/indicator/displayName"))
id <- xmlSApply(parsed_xml_root[["indicator"]], xmlGetAttr, "id")
all_values_df <- cbind(df_xml, id)

我想获取指标的 id 和显示名称。结果数据框为空。请有任何建议

【问题讨论】:

    标签: r xml httr


    【解决方案1】:

    我解决了

    df_xml <- xmlToDataFrame(nodes = xmlChildren(xmlRoot(parsed_content)[["indicators"]]))
    

    【讨论】:

      【解决方案2】:

      您的 xml 文件有一个与之关联的命名空间。因此,首先您需要检索命名空间,然后将其添加到解析表达式中。

      library(XML)
      
      #get namespace
      nsDefs <- xmlNamespaceDefinitions(parsed_content, simplify = FALSE)
      ns <- structure(sapply(nsDefs, function(x) x$uri), names = names(nsDefs))
      
      #rename namespace and add it the node queries
      df_xml <- xmlToDataFrame(getNodeSet(parsed_content, "/x:metadata//x:indicators//x:indicator//x:displayName", c(x=ns)))
      
      #find indicator nodes then get attribute
      nodes <-getNodeSet(parsed_content, "/x:metadata//x:indicators//x:indicator", c(x=ns))
      id <- xmlSApply(nodes, xmlGetAttr, "id")
      
      #put it all together
      all_values_df <- cbind(df_xml, id)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-04
        • 1970-01-01
        • 2015-06-04
        • 1970-01-01
        • 2019-02-04
        • 2018-04-26
        相关资源
        最近更新 更多