I am trying to add lists (
,
) to TextView. Usually you load HTML to TextView with
textview.setText(Html.fromHtml("some String with HTML in it"));
but that does not work for all HTML tags. You can extend support for different tags with overriding default TagHandler and calling it with
textview.setText(Html.fromHtml("some String with HTML in it", null, new MyTagHandler()));
The question is: What does xmlReader in android.text.Html.TagHandler.handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader)
contain? Is it possible to get all data for
(or generally any element with it's contents) somehow using
if (tag.equals("ul")) {
// some call to xmlReader
}
And yes, I have seen those following threads already. I'm just looking for alternate solution.
Answer
The XMLReader is actually only an interface, and the methods it defines can be read from the API documentation.
The ContentHandler that's used to handle the SAX events in the Html class is known as Html.HtmlToSpannedConverter, which, in turn, uses its private handleStartTag(String,Attributes) method to handle the starting tags of HTML elements. Thus the list of out-of-the-box supported elements seems to be br, p, div, em, b, strong, cite, dfn, i, big, small, font, blockquote, tt, a, u, sup, sub, h1...h6 and img. Finally, if the tag is not recognized, the TagHandler given as a parameter to the HtmlToSpannedConverter is used, if it's not null.
The idea of a SAX handler is that you only get events from it: here's a start tag, here's a CDATA block, here's an end tag, etc etc. The other way of handling an XML document is by using a DocumentBuilder to create a Document object of it. SAX parsers are used when you might have a large input stream which might not fit well into the memory, when you need to parse a document quickly or when you are receiving data from somewhere and want to parse it on the fly, before you have received all of it. Thus it is not possible to jump back and forth in an XML stream when using a SAX handler. So, to summarize an answer to your question "Is it possible to get all data for
": no, unfortunately not.
In your TagHandler you will probably want to emit a linebreak when you encounter a opening "ul" tag, then a "•" when you enter an opening "li" tag, and again linebreaks when you encounter a closing "li" or "ul" tag.
If you need to support nested lists, you could add a counter to your TagHandler so that it increments each time you encounter an opening "ul" and decrements when a closing "ul" is encountered. Then you can add different amounts of indentation for the bullet points based on the counter of nested lists.
Another way would be to create a stack of elements by adding an element to it when encountering an opening tag and removing it when encountering a closing tag. By going through the stack it would be easy to count the number of ancestor "ul" elements at any given time.
No comments:
Post a Comment