问题
I know there are a few SAXParser questions there, but I can't find one that describes my problem:
I have a String containing XML data, and I am passing it to a ByteArrayInputStream:
public boolean parse(String message) {
ByteArrayInputStream bis = new ByteArrayInputStream(message.getBytes());
and I call the parse method on it
saxparser.parse(bis, handler); //this row throws a FileNotFoundException
Despite not mentioning any files, the parsing throws a FileNotFoundException
.
Weirdly enough, it's trying to find a filename in my workspace that doesn't exist and later I found that the filename is part of a string enclosed by double quotes (see below, let's call it the "theFile.extension" file)
EDIT: I thought maybe my XML is wrongly written for some reason:
<?xml version="1.0"?>
<!DOCTYPE presence PUBLIC "Some-valid-stuff" "theFile.extension">
Where should I look for a solution in this case?
Thanks
回答1:
The SAX parser is trying to validate the xml using the specified DTD file, and that's the reason it's trying to load that file.
You can disable the validation as below.
saxParserFactory.setValidating(false)
saxParserFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false)
来源:https://stackoverflow.com/questions/33375123/saxparser-throws-filenotfoundexception-despite-not-being-given-a-file-to-parse