What Things Do You Expect To Take 90 Seconds To Complete?
These are some things in the normal course of my life that I have come to expect and embrace to take at most 90 seconds:
- Achieving orgasm with a female
- Doing my taxes
- Thinking about the feelings of others
Here is one thing that I never thought would take 90 seconds:
Executing an Xpath statement against a DOM based document in the Android emulator.
I can hear your thoughts now, “oh well you must have been operating against a ridiculously large and deep XML file”
Not quite. It’s a 20KB XML file which is at its most 3 layers deep. Yet, when attempting to execute 4 independent node selections with Xpath queries (i.e. “/User/Name”), it took 90 seconds from the execution of the first statement to the conclusion of the last one.
In the words of Mommar Gadhafi, “what the fuck?!” I am doing a straight port of an application I had built on WP7, one which pulled data out of a WCF service. Out of all of the problems I thought I would run into in my move to Android, I never thought something as simple as XML parsing would cause me an entire day of headache.
So back to the 90 seconds. Turns out using the Xpath statements along with the DOM object model was a horribly bad idea. Well I am not sure; it could just be the Android emulator is just painfully slow. I ran a method trace on the executing code and the profiler points to the use of Xpath as being very inefficient. (X1000s of calls to push/pull type of methods)
Like any good Microsoft PM, after 3 hours of screwing around trying to get to the bottom of the problem, I said ‘screw it’ and moved on to Plan B: to implement a XmlPullParser to shred the XML and create object instances in memory. It works, and its performant but it’s very ugly. I am talking Old Redmond Way kind of ugly.
I still don’t know what went wrong with the Xpath approach.
However, here is the more important question that lingers in my mind: why the fuck am I having to implement any type of XML Parser in my code?
Did I wake up in 2000? Have they found Chandra Levy yet?
That is Bush league! George W. Bush league.
Fellas, I tell ya, us .NET developers are a spoiled bunch. I mean how long do you spend writing code to serialize/deserialize objects that are returned over a WCF service? If you are like me, then none! I click right click on “Add Service Reference”, type in the URL, and boom: generated code. Not so much in this Android world.
You can bitch and moan all you want about how SOAP is bloated and that it contains a lot of extraneous payload. But come on sons, WSDL and code gen make our lives a hell a lot of easier.
So that was my Friday night. Implementing XML parsing logic to deconstruct a vanilla XML payload.
Now I did some poking on Google, and turns out there are some libraries like Simple and Xstream which do serialization/parsing for you. However, I question why these types of capabilities aren’t part of the platform itself?
I’d imagine a lot of android applications are developed to access web services; does every developer have to go through what I just had to do?
I am a big XmlSerializer hater. I admit it. I like to slam XmlSerializer and its bloated performance when I am blazing a spliff with my homeboys. I mean how can you not? Especially when you got the DataContractSerializer sitting there ready to go. But after this experience, I take back every bad thing I ever said about XmlSerializer.
You never know what you got till it’s gone.