1

I'm getting a JSON response string similar to this:

<strong>B.<\/strong> Because there is no indication of Miss Manette&rsquo;s feelings

The string text that I'm receiving is full of tags like <strong>, <em> and &rsquo; &ldquo; &rdquo; etc. How can I parse it to a plain String with same features?

The only way I could think of is replacing such characters and using Html.fromHtml() method. Is there a built-in parser available? How could I parse such HTML text?

4
  • stackoverflow.com/questions/4432560/… Commented Feb 29, 2016 at 13:33
  • use Html.fromHtml(htmlString).toString() that's pretty easy and in-built as well Commented Feb 29, 2016 at 13:38
  • What exactly do you mean by, parse into a plain string with same features? Do you want the ampersand instead of tags, or tags instead of ampersands? Commented Feb 29, 2016 at 13:48
  • I want to replace the text inside <strong> with bold text. I also want to replace texts like &rsquo; to apostophe etc. Basically, the output text that I want should be user-readable. That's it. Commented Feb 29, 2016 at 13:49

1 Answer 1

3

Use Html.fromHtml only. It'll parse most of the tags supplied and give you the formatted output. The point to note here is that not all of the HTML tags are supported by this method. Checkout this link for more information about what tags are supported. Also check this, though it's a bit old.

If you know what text you'll be parsing, and you have tags that aren't parsed by fromHtml, your best bet would be to replace them with empty string and then use this method.

Sign up to request clarification or add additional context in comments.

Comments

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.