views:

49

answers:

1

I am trying to output a DOM that contains elements such as   to an html file, but it will show   in the output, which is obviously not desirable. My code is as follows

    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.METHOD, "html");
    Result result = new StreamResult(output);
    transformer.transform(new DOMSource(document), result);

is there anyway to get it to just output the   (and no, replacing   in the input is not an option )?

A: 

For HTML to be valid XML including   the document should have a DOCTYPE defining the   entity.

Adding something resembling

<!DOCTYPE document  SYSTEM "document.dtd" [ 
<!ENTITY nbsp "&#160;"> 
]>

at the beginning of the document might do it. But this likely needs to be done to the actual source document, or prepended to it in the stream used for parsing it into the DOM.

If you post some code showing how the document referenced in your line

 transformer.transform(new DOMSource(document), result);

is produced, I might conceivably be able to help more explicitly.

Don Roby