由于最近在學習使用Spring架構,經常會遇到與xml文檔打交道,今天遇到了此問題,特來分享一下解決方案。
出錯原因:
很明顯是因為找不到文件路徑。這個原因是因為我使用了*.clas.getResourceAsStream(xmlFilePath)來進行xml文檔的路徑提供。使用這個方法時,對xml文檔的路徑安放有一定要求,只能在當前路徑下進行搜索,不能使用絕對路徑和相對路徑,所以安放路徑不正確就會產生這個異常。
解決方法:
第一種:
將xml文件放到src文件夾下即可。
第二種:
public static Document parse2Document(String xmlFilePath)throws Exception{
SAXReader reader = new SAXReader();
Document document = null;
File f = new File(xmlFilePath);
InputStream in = new FileInputStream();
document = reader(in);
return document;
}
?
原文:https://blog.csdn.net/CeasarD/article/details/48496199