omi
omi

Reputation: 23

Error in R: no applicable method for 'xpathApply'

I am trying to retrieve data in R from an oData source. This script worked, but after i updated some packages, the script required the xml2 package, which caused an error.

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"))

When determining the names of the attributes i get the following error. Does anyone know what this error message means and how I can solve it?

Error in UseMethod("xpathApply") : no applicable method for 'xpathApply' applied to an object of class "c('xml_document', 'xml_node')"

Upvotes: 2

Views: 6204

Answers (1)

sckott
sckott

Reputation: 5893

httr switched to using xml2 recently in v1.1.0 i think. If you use content(x) on xml data you get an xml2 object back. You can do that, and do something like (not tested)

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

or parse to text like content(x, as = "text"), which gives you character string, then do XML::xmlParse(), then you can proceed as normal with your XML based workflow

Upvotes: 5

Related Questions