2010-06-16 225 views
3

我有變量(不在文件中)的xml文檔。我怎樣才能獲得數據存儲?我沒有任何額外的文件,我有'在'我的源代碼。當我使用如何解析XML文檔?

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
DocumentBuilder db = dbf.newDocumentBuilder(); 
Document doc = db.parse(XML); 

(XML是我的XML變量),我得到一個錯誤

java.io.FileNotFoundException: C:\netbeans\app-s7013\<network ip_addr="10.0.0.0\8" save_ip="true"> File not found. 

回答

5

讀你的XML轉換爲StringReader,在一個輸入源包起來,並傳遞給你的DocumentBuilder:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
DocumentBuilder db = dbf.newDocumentBuilder(); 
Document doc = db.parse(new InputSource(new StringReader(xml))); 
2

假設XML是一個字符串,不要被採用字符串版本混淆 - 的字符串是一個URL,而不是你的輸入!

你需要的是接受輸入流的版本。

你需要創建一個基於字符串的輸入流(我會試着找到代碼示例,但你可以用Google來做)。通常涉及StringReader