开发者

Android decoding html in xml file

In my software im receiving a xml file that is containing some HTML entities like & amp; or whatever. Im successfull decoding the xml but not the HTML entities. The strings are cutted when they meet an html entities... Anybody can help ? I have such code actually to decode the xml...

            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
   DocumentBuilder builder = factory.newDocumentBuilder();
InputStream inputStream = entity.getContent();
Document dom = builder.parse(inputStream);
   inputStream.c开发者_运维问答lose();


   Element racine = dom.getDocumentElement();
   NodeList nodeLst=racine.getElementsByTagName("product");

Does anyone know how i can do the same job, decoding the xml as a dom object and also decoding HTML entities ?

Actually my dom object is not correct because its contain some strings that are cutted because of HTML entities... what can i do ?


I have two approaches to suggest:

  1. Deactivate validation: factory.setValidating(false);

  2. Add a XHTML DTD tag to your XML stream, immediately after the <?xml ...> tag.

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


I think it iss because it detect "'" apostrophe as a final of string. I've founded a solution.

String stringDatosEntrada = new Scanner(urlConnection.getInputStream()).useDelimiter("\\A").next().replaceAll("&amp;#39;","\'").replaceAll("&#39;","\'");

InputStream is = new ByteArrayInputStream(stringDatosEntrada.getBytes());
Document dom = builder.parse(inputStream)


You could try using androids Html tag editor. It should do what you want, it doesn't recognise all HTML but it does seem to work to convert strings:

    Html.fromHtml(inputstream)

Here is a simple example:

    TextView tv = (TextView) findViewById(R.id.tv);
    String s = "<b>This is</b> my first <u>HTML String</u> &amp; it works well!";
    tv.setText(Html.fromHtml(s));

Here is the output:

Android decoding html in xml file

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜