SAXParser throws FileNotFoundException despite not being given a file to parse

醉酒当歌 提交于 2019-12-24 17:23:00

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!