Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

KXML: A Great Find for XML Parsing in J2ME

Enhydra's kXML is a great little XML parser with a small footprint, making it perfect for J2ME apps. It uses a unique method of DOM manipulation and parsing called "pull parsing." Find out whether kXML is must-have for your toolbox.


advertisement
was recently working on a project to develop a multiplayer game for J2ME devices. In this application communication from the server to the device was originally coded as simple key-value pairs separated by ampersands (&), much as Flash handles retrieving variables from servers, but as I began handling more complicated and nested data structures I found this method insufficient. It became difficult to write and error prone.

To solve that problem I immediately decided to recode the transport for the application using XML. XML was a natural choice for me not only because I had used it to transport messages across the network to applets in a previous project, but because XML is easy to debug and write by hand. It also, of course, lets you structure the data in a richer fashion. However, I didn't know that I was about to discover a valuable new gem for my coder's toolbox.

kXML is a compact library designed for use on J2ME devices, though it may be used in other contexts where a small XML parser is needed, for example, with applets. kXML, a project maintained by the Enhydra organization, supports the following features:

  • XML namespace support
  • "Relaxed" mode for parsing HTML or other SGML formats
  • Small memory footprint (21 kbps)
  • Pull-based parsing
  • XML writing support
  • Optional DOM support
  • Optional WAP support

In this article I'll go into detail about a few of these features, specifically pull parsing and DOM manipulation, and I'll tell you how to check the effect of the kXML processing on memory.



Included with this article are two MIDlet examples with full source that show you how to use kXML (download them from the link in the left column). These are KToolBar 1.04 projects and do not include the kXML library—you'll have to get that from http://kxml.enhydra.org/ and put the zip into the "lib" directory of the project.

Working with XML
There are two common ways of working with XML: manipulating the DOM or catching parsing events. Manipulating the DOM is a simple way of interacting with XML where the entire XML tree is parsed into a node structure that resides in memory and you can traverse the tree programmatically. It is very simple to use, but because the entire tree resides in memory—as well as any objects needed to traverse it—it is memory intensive.

In the second method, catching parsing events, the parser traverses the XML data and issues callbacks to a previously registered event listener whenever it encounters particular structures in the data. For example, when the parser encounters an opening tag such as <html> then the event listener would receive an event notifying it of the encounter and pass it any necessary information. A parser that implements such a strategy is called a push parser because the parser is "pushing" the event to a listener.

kXML supports DOM parsing and manipulation but not push parsing. Instead, it uses a slightly different method called "pull" parsing. In contrast to push parsing, pull parsing lets the programmer "pull" the next event from the parser. In push parsing you would have to maintain the state of the current part of the data you were parsing, and based on the events passed to the listener you would have to take care to restore any previous state variables and save new ones when you were changing to a different state. Pull parsing makes it easier to deal with state changes because you can pass parser to different functions, which can maintain their own state variables.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap