
|
July 2005 Introducing XML Parsing with JSR 172
|
|
|
The announcement of the Sony Ericsson W600 Walkman JSR 172 is the standard API for accessing Web Services from a J2ME-enabled mobile phone. An interesting aspect of the JSR 172 API is the introduction of XML packages within the handset's CLDC implementation. Traditionally, XML has been considered primarily suited to the desktop or server and unsuited for resource constrained environments such as mobile phones where CPU and memory are limited. JSR 172 standardizes J2ME XML parsing and in doing so, introduces a lightweight XML parser. JSR 172 implements a subset of JAXP 1.2 (Java API for XML Processing). Essentially it states that the SAX 2.0 (Simple API for XML) specification should be supported with the exception of SAX 1.0 interfaces, DOM 1.0 or 2.0 (Document Object Model) and XSLT (Extensible Stylesheet Language Transformation). This provides for a lightweight API that is suitable for use in a constrained environment. The package structure below mirrors JAXP 1.2 and incorporating XML parsing in your MIDlet is a straight-forward process. javax.xml.parsers org.xml.sax org.xml.sax.helpers To try out XML Parsing, use the XML example and code sample below. Place a sample snippet of valid XML within the emulator's file system so that it can be discovered by the application. The Sony Ericsson J2ME SDK with phone emulators can be downloaded here>> <root drive>\J2ME_SDK\PC_Emulation\WTK2\appdb\SonyEricsson_W600\filesystem\root1 phones.xml <?xml version="1.0"?> In order to perform the actual event based parsing, implement a trivial MIDlet and the DefaultHandler. import java.io.*; public class HelloXML extends MIDlet protected void startApp() FileConnection fc = (FileConnection) Connector.open("file:///root1/phones.xml"); protected void alert(String msg) protected void pauseApp() {} The handler processes the XML, creates a new Phone object and places this in a list. Worth noting is that white space, such as line breaks or tabs, is not ignored and therefore the character methods receives everything between tags. import org.xml.sax.*;
public BasicHandler (HelloXML midlet) public void startDocument() throws SAXException {} public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException tagStack.push(qName); if(chars.length() > 0) Phone currentPhone = (Phone)phones.lastElement(); public void endElement(String uri, String localName, String qName, Attributes attributes) throws SAXException public void endDocument() throws SAXException helloXML.alert(result.toString()); With JSR 172 you can create powerful MIDlets that offer similar functionality to their J2SE counterparts.
| |
Copyright © 2001 - 2009 Sony Ericsson Mobile Communications AB. All Rights Reserved.