1

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

2 Answers 2

1

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()
Sign up to request clarification or add additional context in comments.

4 Comments

Well, you can use annotation tag for customized handling of tags as I've shown here: stackoverflow.com/a/50267693/878126 , but I don't know how to handle both the customized and the basic Html.fromHtml tags working together. Any idea?
@androiddeveloper: No clue, sorry.
Do you know perhaps of a good way to handle unsupported (meaning customized) tags for Html.fromHtml ? I've found some very weird workarounds, using reflection, which I think aren't worth using. This can help for example in the case of customized font tag.
@androiddeveloper: "Do you know perhaps of a good way to handle unsupported (meaning customized) tags for Html.fromHtml ? " -- no, sorry.
0

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.

2 Comments

Exactly. I only need to get the tag text as-is with all attributes and put it in the output. However I only have access to tag name.
A last solution would be to redo Html yourself. Added some details about that.

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.