将InputStream解析为Document Builder解析()

时间:2014-03-19 23:13:23

标签: java inputstream

我正在开发一个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();

我该如何解决?

1 个答案:

答案 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