omi*_*omi 2 xml xpath r odata xml2
我试图从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"))
Run Code Online (Sandbox Code Playgroud)
确定属性的名称时,我得到以下错误.有谁知道这个错误信息的含义以及我如何解决它?
UseMethod("xpathApply")中的错误:没有适用于"xpathApply"的方法应用于类"c('xml_document','xml_node')的对象"
httr我认为xml2最近改用了v1.1.0.如果您content(x)在xml数据上使用,则会xml2返回一个对象.你可以做到这一点,并做一些像(未测试)
xml_find_all(x, '//ns:entry[1]//m:properties[1]/d:*', xml_ns(x))
Run Code Online (Sandbox Code Playgroud)
或者解析为文本content(x, as = "text"),这样可以给你字符串XML::xmlParse(),然后你可以按照XML基础工作流程正常进行