Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android get text from html

I get a special html code:

&lt ;p &gt ;This is &lt ;a href=&quot ;http://www.test.hu&quot ;&gt ;a test link&lt ;/a&gt ;  and this is &amp ;nbsp;a sample text with special char: &amp ;#233;va &lt ;/p&gt ;

(There isn't space before ; char, but if I don't insert space the stackoverflow format it)

It's not a normally html code, but if I paste in a empty html page, the browser show it with normal tags:

<i><_p_>This is <_a_ href="http://www.test.hu">a test link<_/a_> and this is &nbsp;a sample text with special char: &#233;va <_/p_>
</i>

This code will be shown in a browser:

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

So I want to get this text, but I can't use Html.fromHtml, because the component what I use doesn't support Spanned. I wanted to try StringEscapeUtils, but I couldn't import it.

How can I replace special chars and remove tags?

like image 815
Robertoq Avatar asked Apr 03 '11 09:04

Robertoq


People also ask

How can I get HTML text in Android?

If you just want to display some html text and don't really need a TextView , then take a WebView and use it like following: String htmlText = ...; webview. loadData(htmlText , "text/html; charset=UTF-8", null); This does not restrict you to a few html tags either.

Can we use HTML in Android?

Coding? Yes, that's right—coding on your Android device is not only possible, but also popular. The top HTML editors in the Google Play Store have been downloaded millions of times, proving both professionals and enthusiasts increasingly view the operating system as a viable productivity platform.

What does HTML fromHtml do?

fromHtml. Returns displayable styled text from the provided HTML string. Any <img> tags in the HTML will use the specified ImageGetter to request a representation of the image (use null if you don't want this) and the specified TagHandler to handle unknown tags (specify null if you don't want this).

What is Spannable string in Android?

↳ android.text.SpannableString. This is the class for text whose content is immutable but to which markup objects can be attached and detached. For mutable text, see SpannableStringBuilder .


2 Answers

I guess I am too late to answer Robertoq's question, but I am sure many other guys are still struggeling with this issue, I was one of them.

Anyway, the easiest way I found is this: In strings.xml, add your html code inside CDATA, and then in the activity retrieve the string and load it in WebView, here is the 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 may wish to replace é with &eacute ; (note: there is no space between &eacute and the ; )

Now, in your activity, create WebView and load string st1 to 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 useful I will be greatful if you can mark it as answered, so we help other struggling with this issue

like image 67
m.othman Avatar answered Oct 16 '22 12:10

m.othman


Write a parser, no different than you would in any other situation where you have to parse data.

Now, if you can get it as ordinary unescaped HTML, there are a variety of open source Java HTML parsers out there that you can use. If you are going to work with the escaped HTML as you have in your first example, you will have to write the parser yourself.

like image 40
CommonsWare Avatar answered Oct 16 '22 12:10

CommonsWare