【发布时间】:2010-05-17 10:14:30
【问题描述】:
又过了一天,SAX、Java 和朋友们又出现了一个奇怪的错误。
我需要遍历 File 对象列表并将它们传递给 SAX 解析器。但是,解析器由于IOException 而失败。但是,各种File 对象方法确认该文件确实存在。
我得到的输出:
11:53:57.838 [MainThread] DEBUG DefaultReactionFinder - C:\project\trunk\application\config\reactions\TestReactions.xml
11:53:57.841 [MainThread] ERROR DefaultReactionFinder - C:\project\trunk\application\config\reactions\null (The system cannot find the file specified)
所以问题显然是第二行中的null。我已经尝试了几乎所有将文件作为参数传递给解析器的变体,包括String(都来自getAbsolutePath()和手动输入),URI,更奇怪的是,作为@ 987654329@(对于这个我得到了同样的错误,除了整个相对路径被报告为空,所以C:\project\trunk\null)。
我能想到的只是SAXParserFactory 配置不正确。不过,我不知道出了什么问题。
这里是相关代码:
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(true);
try {
parser = factory.newSAXParser();
}
catch (ParserConfigurationException e) {
throw new InstantiationException("Error configuring an XML parser. Given error message: \"" + e.getMessage() + "\".");
}
catch (SAXException e) {
throw new InstantiationException("Error creating a SAX parser. Given error message: \"" + e.getMessage() + "\".");
}
...
for (File f : fileLister.getFileList()) {
logger.debug(f.getAbsolutePath());
try {
parser.parse(f, new ReactionHandler(input));
//FileInputStream fs = new FileInputStream(f);
//parser.parse(fs, new ReactionHandler(input));
//fs.close();
}
catch (IOException e) {
logger.error(e.getMessage());
throw new ReactionNotFoundException("An error occurred processing file \"" + f + "\".");
}
...
}
我没有特别规定提供自定义 SAX 解析器实现:我使用系统默认值。任何帮助将不胜感激!
编辑更多信息:
我使用流时的代码:
FileInputStream fs = new FileInputStream(f);
InputSource is = new InputSource(fs);
//is.setSystemId(f.toURI().toString());
parser.parse(is, new ReactionHandler(input));
提供输出
11:07:10.703 [MainThread] DEBUG DefaultReactionFinder - C:\project\trunk\application\config\reactions\TestReactions.xml
11:07:10.706 [MainThread] ERROR DefaultReactionFinder - C:\project\trunk\application\null (The system cannot find the file specified)
这表明XML文件的相对目录没有正确解析。如果我包含注释掉的行,则相关目录将再次正确解析。这让我觉得某个地方的某些设置不正确......
【问题讨论】: