无法为签名'"字符"'找到函数'saveXML'的继承方法

时间:2014-06-09 10:42:21

标签: xml r

尝试解析xml文件时,我总是会遇到此错误。

重现它的简单脚本:

doc <- xmlRoot(xmlTreeParse("http://www.stat.purdue.edu/~mdw/490M/cdcatalog.xml"))

xpathSApply(doc, "//CATALOG/CD/PRICE", xmlValue)

Error in (function (classes, fdef, mtable)  : 
 unable to find an inherited method for function ‘saveXML’ for signature ‘"character"’

加载以下包:

sessionInfo()
 R version 3.1.0 (2014-04-10)
 Platform: x86_64-apple-darwin13.1.0 (64-bit)

 locale:
 [1] de_DE.UTF-8/de_DE.UTF-8/de_DE.UTF-8/C/de_DE.UTF-8/de_DE.UTF-8

 attached base packages:
 [1] stats     graphics  grDevices utils     datasets  methods   base     

 other attached packages:
 [1] XML_3.98-1.1

 loaded via a namespace (and not attached):
 [1] tools_3.1.0

3 个答案:

答案 0 :(得分:28)

url <- "http://www.stat.purdue.edu/~mdw/490M/cdcatalog.xml"    
doc <- xmlRoot(xmlTreeParse(url, useInternalNodes = TRUE))
xpathSApply(doc, "//CATALOG/CD/PRICE", xmlValue)

我认为这与命名空间的解析有关。 xpath需要useInternalNodes = TRUE才能使用getNodeSet()

答案 1 :(得分:1)

我做了同样的事情以获得相同的错误。但我意识到我错过了 useInternalNodes = TRUE 。当我把它包含在

中时

答案 2 :(得分:0)

doc <- xmlParse("http://www.stat.purdue.edu/~mdw/490M/cdcatalog.xml")

应该这样做......