r - 使用 R 中的 xmlToDataframe 从 xml 数据返回的空数据框
问题描述
处理使用 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&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 转换为数据框,如下所示:
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 和显示名称。结果数据框为空。请任何建议
解决方案
您的 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)
推荐阅读
- python - 为什么在这个简单的 python sqlite3 更新查询代码上出现“数据库已锁定”错误?
- javascript - 通过 JSON 键的 Node Js 转换 JSON
- android - 单击时如何标记抽屉导航中的子菜单?
- pointers - 你能用汇编语言减去两个指针吗?
- amazon-web-services - AWS Amplify Auth + React Native - 电话号码的属性值不能为空
- excel - Excel 日期的 SQL Developer 导出编码
- swift - 在 UITextView 中阅读更多和更少阅读:Ilya Puchka readMoreTextView not working
- ruby-on-rails - Docker-compose 无法翻译主机名“postgres”
- reactjs - React setState 未能捕获被拒绝的 Promise
- python - curl 不检索 url。错误 (56) OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 10054