【发布时间】:2013-10-04 17:34:05
【问题描述】:
我在读取 xml 文件时遇到以下问题,当使用下面的 dirFile 时,我收到以下错误:
C:\Folder\folder1\folder2\project\resources\xmls\file.xml(系统找不到指定的路径)
并且文件在那里并且提到的路径是正确的。但是如果我使用这个路径,它曾经是 C:\repo\eclipse\testcases\file.xml 的作品。我想在项目中包含 xml。
private static String dirFile = "resources/xmls/";
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(dirFile + file);
我们将不胜感激。
【问题讨论】:
-
如果错误是
C:\Folder\folder1\folder2\project\resources\xmls\file.xml (The system cannot find the path specified),那么你在C:\Folder\folder1\folder2\project\resources\xmls\file.xml没有文件 -
我还会考虑更合理的文件夹命名约定。
-
我猜 DocumentBuilder.parse() 方法没有按照您的预期走。我不确定它应该如何精确,但如果你使用绝对路径而不是相对路径,并且如果它有效,那么你应该更改“dirFile”的内容。