2017-02-16 144 views
1

我想解析文檔生成器中有一個標籤(#)的XML路徑,它不起作用,似乎解析器忽略#包含之後的所有內容。有誰知道我能做什麼?DocumentBuilder解析路徑和標籤

這是代碼:

Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(xml.getAbsolutePath()); 

XML路徑是這樣的:C:\用戶\米魯娜。\ E#abc.xml 的錯誤,我得到:java.io.FileNotFoundException: C:\Users\miruna\e

回答

0

你沒有明確說明你的變量xml的類型。但它似乎是

File xml = ...; 

您正在使用方法DocumentBuilder.parse(String uri) 這需要一個URI字符串作爲參數。
URI中,#具有特殊含義, 從片段中分離路徑。因此你會得到所描述的問題。

要修復它,您可以使用方法DocumentBuilder.parse(File f),該方法將File作爲參數。

Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(xml); 

,或者你可以堅持到parse(String uri)方法和修復從文件到URI字符串

Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(xml.toURI().toString()); 
+0

喜的轉換,並感謝您的回答。它可以將xml解析爲文件。 :) –

+0

@MiruOrşa歡迎。如果這解決了你的問題,你應該接受答案。 –