2014-03-07 48 views
1

我正在使用R解析XML文件,但我總是收到錯誤消息,說XML內容似乎不是XML,但是XML文件確實包含XML數據,即使我只是把一個簡單的標籤。請參閱下面的示例R:XML內容似乎不是XML

有關如何解決此問題的任何建議?

R代碼裏面:

xmlinfile = "sample.xml" 
xml = xmlInternalTreeParse(xmlinfile) 

sample.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<root> 
    <name>Sample</name> 
</root> 

錯誤:

Error: XML content does not seem to be XML: 'sample.xml' 

按照要求,下面是我得到了什麼,當我解析之前打印的文件:

xmlinfile = "sample.xml" 
sampleFile<-file(xmlinfile,"w") 
print(sampleFile) 

description  class  mode  text  opened  can read can write 
"sample.xml" "file"  "w" "text"  "opened"  "no"  "yes" 

提前致謝。

+0

相關,但可能不是重複:http://stackoverflow.com/questions/17954817/xml -content-does-not-seem-to-be-xml?rq = 1 – 2014-03-07 02:09:21

+0

你可以在解析它之前打印文件嗎?你確定它的開放* *特定的'sample.xml'嗎? – 2014-03-07 02:09:51

+0

@LegoStormtroopr,我解析後打印文件,並得到:> print(xml) [1]「錯誤:XML內容似乎不是XML:'sample.xml'\ n」 attr(,「class」 ) [1]「try-error」 attr(,「condition」) Ianthe

回答

2

感謝所有的意見和答案,我想我找到了答案。我的XML文件與導致問題的R腳本位於不同的位置。

但是,錯誤信息似乎並不正確或直觀。但無論如何,它現在起作用。 再次感謝大家。 ;)

+0

我有同樣的問題,並通過使用'setwd()'解決它我的本地XML文件所在的文件夾。 – salvu

1

遺憾地說,但只是工作:

> library(XML) 
> xmlinfile = "sample.xml" 
> xml = xmlInternalTreeParse(xmlinfile) 
> str(xml) 
Classes 'XMLInternalDocument', 'XMLAbstractDocument' <externalptr> 
+0

你跑哪裏了?我正在使用RStudio,有什麼區別嗎? – Ianthe

+1

我在命令行的R中運行它,但它在RStudio中會一樣:)如果再次出現,請記住getwd()和setwd()命令,它們爲R會話設置工作環境。 – earino

+0

感謝earino。 :) – Ianthe

1

它的工作原理是這樣 - >下載1,第2解析

file_q4<-"https://d396qusza40orc.cloudfront.net/getdata%2Fdata%2Frestaurants.xml" 
discFile_q4<-"./data/q4_dat.xml" 
download.file(file_q4, discFile_q4, method = "wget",quiet = TRUE) 
dat_xml<-xmlInternalTreeParse(discFile_q4) 
rootNode<-xmlRoot(dat_xml) 

完成