我正在开发一个Java程序,将此Document显示为字符串,我收到以下错误:
The entity "nbsp" was referenced, but not declared.
Java代码是:
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
fDocBuilder = docFactory.newDocumentBuilder();
URL url = new URL(fServer + query);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
InputStream responseStream = connection.getInputStream();
Document response = fDocBuilder.parse(responseStream);
return response.toString();
我该如何解决?
答案 0 :(得分:1)
XML只支持几个实体,nbsp
肯定不是其中之一。为了使您的代码运行,您应该在DTD中声明您的entites的含义。
您可以在文档中添加以下声明:
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
如果您对文档没有任何影响,那么您可以使用EntityResolver2
为此提供界面。
请参阅:EntityResolver2 - 方法getExternalSubset