R 中的错误:"xpathApply"没有适用的方法



我正在尝试从oData源检索R中的数据。此脚本有效,但是在我更新了一些包后,该脚本需要 xml2 包,这导致了错误。

library('httr') # for sending http requests
library("xml2") # for reading xml
# log start of request
log_message(paste("Requesting OData from:",url))
# get the OData resource
response <- GET(url,authenticate(usr,pwd))
# parse xml docucument
responseContent <- content(response,type="text/xml")
# determine the names of the attributes
xmlNames <- xpathApply(responseContent,
                        '//ns:entry[1]//m:properties[1]/d:*',xmlName, 
                        namespaces = c(ns = "http://www.w3.org/2005/Atom",
                                       m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata",
                                       d="http://schemas.microsoft.com/ado/2007/08/dataservices"))

确定属性名称时,出现以下错误。有谁知道此错误消息的含义以及如何解决它?

使用方法("xpathApply") 中的错误: 没有适用于"xpathApply"的方法应用于类"c('xml_document', 'xml_node')"的对象

我认为

httr最近在v1.1.0中改用xml2。如果对 xml 数据使用content(x),则会返回xml2对象。你可以这样做,做一些类似的事情(未经测试)

xml_find_all(x, '//ns:entry[1]//m:properties[1]/d:*', xml_ns(x))

或解析为文本,如 content(x, as = "text") ,它为您提供字符串,然后执行XML::xmlParse(),然后您可以正常进行基于 XML 的工作流程

相关内容

最新更新