最近用dom4j寫了一個修改XML文件的小例子,拋出了如下異常:
org.dom4j.DocumentException: unknown protocol: d Nested exception: unknown protocol: d
在網上查了資料,問題的原因是Tomcat的安裝路徑有空格.d是Tomcat安裝的盤符.
解決的辦法有兩種:
1.重新安裝Tomcat.去掉空格.
2.將解析的XML文檔轉換為File類型.
原始代碼:
String path = "D:\\config.xml"; SAXReader reader = new SAXReader(); Document document = reader.read(path);
目的代碼:
String path = "D:\\config.xml"; File file = new File(path); SAXReader reader = new SAXReader(); Document document = reader.read(file);