Question

I am working on a Java program to display this Document as a string and I am getting the following error:

The entity "nbsp" was referenced, but not declared.

The Java code is:

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();

How do I fix that?

Was it helpful?

Solution

XML supports just a few entities, and nbsp is certainly not one of them. To make your code running you should declare a meaning of your entites in DTD.

You can add following declaration to document:

<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 

If you don't have any influence on a document, then you can use EntityResolver2 which provides interface for this.

See: EntityResolver2 - method getExternalSubset

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top