Donnerstag, 7. Januar 2010

Android and XML

As mentioned in an earlier post, the goal is to run an 8-Puzzle game on Android and let Prolog agents analyze the moves. The communication between Prolog and Android should be achieved by using the SQLSpaces. If you want to read more about SQLSpaces go here or more general here. The SQLSpaces communication is using a XML protocol over sockets. The server is implemented in Java and client libraries exist in Java (of course), Prolog, C#, PHP and ruby.

The first though was, hey, client lib is in Java, so most probably it should work on Android! Unfortunately not! The problems was, that the latter versions of the SQLSpaces client have used StAX for parsing the incoming XML messages and DOM to generate XML for requests to the server. However, StAX is not supported on Androids - javax.xml.stream.* is not provided by the Android API. Yet DOM is only implemented in level 2! The result was, that we had change the SQLSpaces client to use DOM level 2 only and extract the XML parsing and make it adaptable. The first issue was not a big deal. We only had to remove some convenient access methods to Text nodes and some minor issues. The parsing was a bigger story!

Android supports two ways of parsing XML: SAX and XMLPull (of course you can also use DOM but this comes as always with a huge memory overhead)! It is recommended to use the SAX parser over XMLPull parser, as the latter one is slower. Actually, the decision depends on what you need: Speed vs. a simple way for parsing XML. You can find a very nice example presenting Android and all kinds of XML processing at IBM.

Finally, we have now a very nice mechanism for the SQLSpaces that will choose the best suited XML parser on the Desktop and on Android. We haven't compared speed yet, we are first happy that it is working at all and in a very short time.

Keine Kommentare: