tags:

views:

879

answers:

3

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?

+1  A: 

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

Kevin Tighe
+2  A: 

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)

DroidIn.net
I'm planning on writing a tutorial for SAX parsing of XML feeds in Android on Androidguys.com. Time permitted - by the end of the week, so stay posted
DroidIn.net
+1  A: 

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

gabe
So, you're saying a proxy does the parsing, and you return data in a native format for the android app? That's pretty slick, but I actually own both ends of the transaction. What I'm looking for is how to serialize and deserialize my objects. In C# I can do this declaratively with attributes...does the same exist in Java?
Rich
For serializing we use hessian http://hessian.caucho.com/ there exists a android lib for this called hessdroid and a lib for c# too. If the data is not too big you could use json http://json.org/ for transport. In android exists a package to deal with json data http://developer.android.com/reference/org/json/package-summary.html
gabe
Hessian looks perfect for me actually. I can just change my service signatures to pass strings around and put Hessian on both ends to serialize/deserialize. So far, documentation looks pretty shotty for Hessian...any good links with documentation or which classes I should look at for just serializing and deserializing objects?
Rich
Actually you can pass any object you like. Just make the interfaces known on both sides.
gabe