Question

I'm learning the Android api from a book, and it seems like there isn't any mention of a stream-lined api for dealing with raw xml (reading and writing). His suggestion for parsing is the XmlPullParser, and his examples look horrendous considering the kind of api's I'm spoiled by in other platforms (LINQ to XML especially).

Is this the best available technique on the Android platform?

Obviously I can write a wrapper to avoid the repetitive stuff, but I'd be surprised if no such thing already exists.

Also, he doesn't even make mention of creating xml structures in code. What are my options for both?

On a side note, do any Java devs that are familiar with LINQ to XML in .Net know of anything equivalent in Java?

Was it helpful?

Solution

Since you probably don't want to load any substantial size DOMs into Android's memory - pull and SAX parsers are preferred way dealing with XML in Android. I think it pays to invest into understanding how SAX works and write a custom handler than rely on some generic libraries that may be incompatible or overbloated. I parse XML in my apps using SAX all the time and I'm very pleased with the speed (most of the time)

OTHER TIPS

Well I'm pretty new to Java, but here's what I've gleaned so far about xml parsing on Android:

The XmlPullParser approach is recommended for Android due to resource constraints. There is a DOM parser available in Android, which would let you use XPath to navigate an xml document. Using the DOM means that you have to load the entire document into memory at once, however. The XmlPullParser method is much more efficient in terms of memory used.

The XmlPullParser method takes a little getting used to after being comfortable with LINQ to XML or XPath, but it's really not too bad IMHO (at least with the documents I was parsing). If you're working with small xml documents you could certainly use the DOM with XPath.

There's a decent article about the different methods for reading and writing XML with Android here: http://www.ibm.com/developerworks/opensource/library/x-android/index.html

I had the same issues with parsing xml or xhtml and ended up writing a webservice doing it for me.

Android Device ->(Request URL) -> Webservice Get and Parse -
-> (Data) -> Android Device

You can transmit the data in JSON to work with it on the device. The advantage of this is you can minimize the traffic on the slow mobile network and change the parsing without releasing a new android app.

Maybe this is will work for you too.

regards

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top