SAX parser does not resolve filename

Another day, another strange bug with SAX, Java and friends.

I need to loop over a list of objects File

and pass them to the SAX parser. However, the parser fails due to IOException

. However, various methods on the object File

confirm that the file does exist.

The result I am getting:

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)

      

So the problem is obviously that null

in the second line. I have tried almost all options for passing a file as a parameter to the parser, including both String

(both from getAbsolutePath()

and manually entered) URI

and, even weirder, how FileInputStream

(for this, I get the same error, except that all relative path is reported as null, so C:\project\trunk\null

).

All I can think of is misconfiguration SAXParserFactory

. I don't even know what is wrong.

Here is the code:

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 + "\".");
    }
    ...
}

      

I made no special provisions for providing a custom SAX parsing implementation: I am using the default system. Any help would be greatly appreciated!

Edit More information:

My code when I use streams:

FileInputStream fs = new FileInputStream(f);
InputSource is = new InputSource(fs);
//is.setSystemId(f.toURI().toString());
parser.parse(is, new ReactionHandler(input));

      

What gives the conclusion

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)

      

which shows that the relative directory of the XML file is not resolved correctly. If I include a line that is commented out, the relative directory is resolved correctly again. This makes me think that somewhere some setting is wrong ...

+2


a source to share


1 answer


Wow, I should know ... the problem is not in the code, but in the definition DOCTYPE

in the XML file! I gave a public ID, but not a system ID. This means that the file the parser was unable to resolve was not the XML file itself, but rather a DTD file!



0


a source







All Articles