Android get text from html

I get a special html code:

<p> This is & hairsp; ? hairsp; ? hairsp; ? hairsp; ? hairsp; ? hairsp; ? hairsp; http: //www.test.hu& "→ test link & lt; / a &. and this is a sample text with a special char: & # 233; va </p> gt;

(there was no place before; char, but if I do not insert a space in the stackoverflow format)

This is not normal html code, but if I embed it on an empty HTML page, the browser will display it with the usual tags:

<p> This is a <a href = "http://www.test.hu"> test link </a>, and this is an example text with a special char: éva </p>

This code will be displayed in the browser:

This is a test link. This is a sample text with a special char: éva

So, I want to get this text, but I cannot use Html.fromHtml , because the component that I use does not support Spanned . I wanted to try StringEscapeUtils , but I could not import it.

How to replace special characters and remove tags?

+6
java android html tags
source share
2 answers

Write a parser that is no different from what you could in any other situation when you need to analyze data.

Now, if you can get it as plain unescaped HTML, there are many open source open source Java parsers that you can use. If you are going to work with escaped HTML, as in the first example, you will need to write a parser yourself.

+1
source share

I think I'm too late to answer Roberta’s question, but I’m sure that many other guys are still struggling with this problem, I was one of them.

In any case, the easiest way to find this: In strings.xml, add your HTML code inside CDATA , and then in the process of getting the string and loading it into the WebView , here is an example:

in strings.xml:

 <string name="st1"><![CDATA[<p>This is <a href="http://www.test.hu">a test link</a> and this is a sample text with special char: éva </p>]]> </string> 

you can replace é with amac; (note: between and amacute and ; ) there is no space

Now in your activity, create a WebView and load the string st1 into it:

 WebView mWebview = (WebView)findViewById(R.id.*WebViewControlID*); mWebview.loadDataWithBaseURL(null, getString(R.string.st1), "text/html", "utf-8", null); 

And horraaa, it should work correctly. If you find this post helpful, I will be grateful if you can mark it as an answer, so we help others struggling with this problem.

+1
source share

All Articles