2

我正在尝试从 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"))

在确定属性的名称时,我收到以下错误。有谁知道此错误消息的含义以及我该如何解决?

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

4

1 回答 1

5

httr我想xml2最近改用了。v1.1.0如果你content(x)在 xml 数据上使用,你会得到一个xml2对象。你可以这样做,并做类似的事情(未测试)

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

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

于 2016-02-18T15:02:51.600 回答