basv
basv

Reputation: 455

Android: Html.fromHtml() handle non-standard html tags

I am parsing an html message which may contain user-defined tags, e.g. <usertag uservalue="value" />.

I am using standard Html.fromHtml() function to parse the html source. Unfortunately it simply ignores non-standard tags and remove these from the output. I would like to keep them.

I've tried to supply my own TagHandler to the fromHtml() function, but I do not know what to do inside the handleTag() function. Looks like I do not have access to the non-standard tag attributes\content from the TagHandler.handleTag() function? How do I use xmlReader passed inside fromHtml()?

Thanks

Upvotes: 1

Views: 2401

Answers (2)

CommonsWare
CommonsWare

Reputation: 1007554

I am parsing an html message which may contain user-defined tags, e.g. <usertag uservalue="value" />.

Then you do not have HTML.

Here are some options:

  1. Get rid of the "user-defined tags" outright
  2. Have your source markup be valid HTML, with your "user-defined tags" turned into valid HTML (e.g., using <div> and <span> with class attributes, as the microformats people do)
  3. Pre-process your markup to handle your "user-defined tags", turning them into valid HTML, before calling Html.fromHtml()

Upvotes: 1

Valentin Rocher
Valentin Rocher

Reputation: 11669

From what we can see in the javadoc, there seems to be 4 parameters :

  • opening, specifying if this is an opening or closing tab
  • tag, presumably the tag name
  • output, which is the generated Editable to which you'll add your data
  • xmlReader, the current XML reader.

It looks like you can't use the attributes, though.

One last solution, although a little bit complicated, would be to reimplement Html to suit your needs. You can find its source here.

Upvotes: 0

Related Questions