1

I have an email body and it has html,css tags and I am trying to retrieve only the body text in Android.

I used;

String.valueOf(Html.fromHtml(body);

and that gave me the text with css tags. What should I do for retrieve only the text? Result is holding in a String

Normally (when I first receive body );

<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Tahoma
}
--></style></head>
<body class='hmmessage'><div dir='ltr'>
TEXTONE<div>TEXTTWO</div>                     </div></body>
</html>

After String.valueOf(Html.fromHtml(body);

<!-- .hmmessage P { margin:0px; padding:0px } body.hmmessage { font-size: 10pt; font-family:Tahoma } -->

TEXTONE
TEXTTWO

I just want to receive TEXTONE and TEXTTWO

2
  • Can you please post HTML example? Commented Apr 23, 2012 at 19:32
  • I edit the Html in the question Commented Apr 23, 2012 at 20:00

1 Answer 1

1

fromHtml() supports only common HTML tags. HTML comments are not considered as tags, and are ignored. List of the tags, supported by fromHtml() can be found here.

Following code should fix your problem:

s = String.valueOf(Html.fromHtml(body);
s = s.replaceAll("(?s)<!--.*?-->", "");
Sign up to request clarification or add additional context in comments.

3 Comments

"Android does not support stripping HTML comments via fromHtml()." I can strip my text from html with this codeline
You can your strip html tags, but not comments. This - <!-- O.o --> - is the HTML comment.
And yes, stripping = deleting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.