1

I want to make an app that fetches XML documents from the web that need to be interpreted and rendered, for example:

<doc>
    <entity type="person">You</entity> can also look at <ref to="A">A</ref> or <ref to="B">B</ref>.
</doc>

The documents have a pretty complex DTD and a structure that is not static and likely contains thousands of tags.

Let's say I want the ref elements to be rendered as clickable elements and for the time being ignore the other tags (just show them as ordinary text).

Should I scan the file with an XmlPullParser, create an internal data structure out of the document and create TextViews for all text sequences and make ref tags clickable TextViews? (This is the method described in the Android dev resource 'Parsing XML Data'.)

Or is it better, performance and UX wise, to convert the document to HTML and use a WebView? Or do some other parsing trick, like use SimpleXML instead of XmlPullParser to deserialize the XML?

Maarten
  • 6,894
  • 7
  • 55
  • 90
  • 2
    Convert input to HTML and display it in a WebView ;) – ChristopheCVB Apr 01 '13 at 20:13
  • 1
    Uh...use the XML Android already uses for UI's...? –  Apr 01 '13 at 20:13
  • So you mean loading in an XML document and converting it into an Android layout (XML) document and inflating it at runtime? Do you think that is efficient, especially when your document contains thousands of tags? – Maarten Apr 01 '13 at 20:37
  • Why the downvotes? It seems like a pretty legit question to me. – Maarten Apr 01 '13 at 20:39
  • It's a totally "legit" question, but I suspect it is indicative of a fundamental misunderstanding about how Android XML works. See my answer for more details. – Yevgeny Simkin Apr 01 '13 at 20:41
  • I feel misunderstood myself... I guess my question was a bit misleading before. Is it clearer now? – Maarten Apr 01 '13 at 20:46
  • It is, but I believe my edited answer will let you know that you can't do what you're hoping to do. And why the down-vote?? Seriously folks, if you're going to down-vote an answer, have the courtesy of adding a few words about what you felt was wrong with it. – Yevgeny Simkin Apr 01 '13 at 20:49
  • Actually I was a sarcastic when I answered this dude telling me to use 'XML Android'. I know parsing XML is notoriously slow but it is something that has to happen because I'm dealing with this web service that only deals my data in XML. – Maarten Apr 01 '13 at 20:52

2 Answers2

0

The larger question is "when" are you doing this? The XML in the apk doesn't stay as xml, it's compiled, so, loading the xml that you're describing at run time is pointless since when all's said and done you're going to have to translate it into a bunch of dynamic display code.

Or are you saying you are writing an app that takes your xml and then converts it into Android style UI XML that you're going to cut and paste into your project?

If you are planning on simply using a WebView then you don't have to worry about any native Android UI widgets (i.e. TextView) and you can render your XML in whatever way you wish, however, I suspect that's not what you're asking, and I further suspect that you're somewhat confused about the nature of Android's XML usage.

Or are you hoping to convert your XML into Android native XML and then inflate it at run time? (that's not possible at this time as per the documentation: "For performance reasons, view inflation relies heavily on pre-processing of XML files that is done at build time. Therefore, it is not currently possible to use LayoutInflater with an XmlPullParser over a plain XML file at runtime; it only works with an XmlPullParser returned from a compiled resource (R.something file.)" read more here ).

I think you'll need to clarify your question a bit in order to get a useful answer.

Yevgeny Simkin
  • 27,946
  • 39
  • 137
  • 236
  • Lot of people downvoting tonight... What's up with that? Anyway, I am saying that I want to convert XML that I get through a web service into Android Views, and because there's a lot of tags, I want to know what is efficient and if there is anything I should look out for. That's all. – Maarten Apr 01 '13 at 20:50
  • I understand your desire, however, that's just not possible. Your only alternative is to create your own parser/renderer, that walks your XML and manually adds Views (of whatever type) based on your XML's definitions. The question then becomes, what sort of UI are you building and can you build it in HTML instead (and display it in a WebView) because that approach is a LOT less heavy handed. If not you're going to have to give some very serious thought as to how you're going to emulate the android XML in your own, so as to cover all the bases. – Yevgeny Simkin Apr 01 '13 at 20:52
  • Ehhhh, well I know it is possible because that is what the [XML parser example](http://developer.android.com/training/basics/network-ops/xml.html) does. I'm just saying, there's multiple possibilities and I want to know the best one in my case. – Maarten Apr 01 '13 at 22:49
  • Your question explicitly states that you want to create TextViews for all the relevant text bits. So, how do you intend to lay those text views out? My advice was based on the assumption that you want your app to actually do something useful and not just parse some XML (which of course *is possible, that's not what I was saying was not possible). Your proposed solution will not do what your question says you want. – Yevgeny Simkin Apr 02 '13 at 02:48
  • What I ask is 'Should I scan the file [...] and create TextViews for all text sequences?' because I know this is one possible solution to my overall question 'How should I render a very complex XML document?'. I knew the View hierarchy would be crazy if I had a separate TextView for every XML tag. I would end up with thousands of sibling TextViews. So I turned to StackOverflow to ask for advice. See if there was another way, which there is: use TagHandler to handle the styling within a single TextView. Evidently, I was too implicit in this, so sorry for the confusion. – Maarten Apr 02 '13 at 20:25
  • But the question remains... 'why' use a convoluted TextView (even if it's just one) vs. a WebView? – Yevgeny Simkin Apr 03 '13 at 02:28
0

Showing the XML together with some CSS in a WebView is easiest.

Alternatively, you can do pre-processing with XmlPullParser, and for rendering/styling you can use Android's Html class coupled with a custom implementation of TagHandler and/or do some stuff with Spannable. (See this answer about using Spannable Strings.) Use TextView.setText(Html.fromHtml()).

Community
  • 1
  • 1
Maarten
  • 6,894
  • 7
  • 55
  • 90