Search code examples
androidxhtmlhtml-encode

XHTML encode Android


I need to XHTML encode some text in Android.

I had been using Html.escapeHtml().

However Html.escapeHtml(" ") generates "  ". I need it to generate " &#160".

Is there an equivalent? I need like a Xhtml.escapeXhtml().

Or should I just do string replacements?


Solution

  • Ah, I just realized the problem is that the document is missing the DTD. I need to add

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

    to the top of the document

    and then &nbsp; is valid.

    Similar answer here: Undefined entity &nbsp XML parsing exception