没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
Java Solaris Communities My SDN Account Join
SDN
» search tips
APIs Downloads Technologies Products Support Training Sun.com
Developers Home > Technical Topics > Mobility > MIDP > Reference > Technical Articles and Tips >
Article
Parsing XML in J2ME
Print-friendly Version
by Jonathan Knudsen
March 7, 2002
Download:
[source code] [MIDlet suite JAD] [MIDlet suite JAR]
Note: This article contains essentially the same information as JavaOne session #2716,
Parsing XML in J2ME, presented March 28, 2002.
The convergence of J2ME and XML is currently a handful of open source parsers. In this article you'll
learn how to parse XML in a MIDP client application. I'll begin by talking about system architecture and
the motivation for using XML as a data transport. Then I'll describe the available XML parsers, discuss
the challenges of developing in a small environment, and present some sample code.
Multi-tier System Architecture
To understand why you might want to parse XML on a J2ME device, let's first examine the architecture
of a typical multi-tier application. Multi-tier is one of those ubiquitous terms that means something
different to just about everyone. I'll nail it down with a fairly specific architecture, as shown in the
following figure:
Typical multi-tier architecture
The current world is web-centric, so systems are often designed with HTML browsers as the clients. The
client performs very little of the application processing and functions as a fancy kind of terminal. The bulk
of the application runs on server which uses a database for persistent storage.
As the wireless world began expanding, server vendors found that they could conveniently support
wireless devices by adding support for WAP browsers. The underlying paradigm of the browser as front
Skip to Content
Parsing XML in J2ME
file:///H|/20070611/web%20pages/Parsing%20XML%20in%20J2ME.htm (1 of 11)2007-6-12 21:27:30
Search
Parsing XML in J2ME
end to the application remains unchanged; the server is just serving WML over WAP in addition to HTML
over HTTP.
The diagram also shows a standalone client, which could communicate with the application on the server
in several different ways. The client could make HTTP connections, use RMI to manipulate remote
objects, or implement a customized protocol. The chief advantage of having a standalone client in place
of a browser is the chance to provide a richer user interface. The main disadvantage is the difficulty of
client installation and maintenance.
Where do MIDP clients fit in this picture? Keep in mind that with MIDP devices, everything is small, and
this affects their utility as application clients.
● Network connection setup is slow.
● Data rates are slow.
● The processor is slow.
● Memory is scarce.
Because of these constraints, MIDP client applications should be designed to be as small as possible. At
the same time, they can feature a smooth and capable user interface that goes far beyond the user
experience offered by a WAP browser.
MIDP client have one other important characteristic as compared to WAP applications; the application
can run offline and make updates to the server periodically. This is especially important with wireless
networks, which are slower and less reliable than the desktop network you may be accustomed to. WAP
applications, by contrast, require a network connection as the user moves from screen to screen.
The following figure shows one possible implementation of a multi-tier system that supports HTML
browsers, WAP browsers, standalone clients, and MIDP clients.
A multi-tier architecture with XML
The figure shows one way of supporting multiple client types. Instead of creating custom server-side
code for each client type, you write generic code that returns data as XML documents. Then you create
transformations (using XSLT) that change the basic XML documents into whatever is required for the
client device. Once set of transformations produces HTML for desktop browsers, while another set might
produce the WML to support WAP browsers.
But what kind of data do you send to the MIDP client? You send whatever you want, of course, anything
from plain text to binary data. If you're using XML on the server side, however, you may consider XML
itself as a data exchange format. It makes your life pretty easy on the server side, for one thing. You
might be able to send your basic XML documents unchanged, or you could create some simple
transformations to send a more terse XML format to the MIDP device.
file:///H|/20070611/web%20pages/Parsing%20XML%20in%20J2ME.htm (2 of 11)2007-6-12 21:27:30
Parsing XML in J2ME
Sending XML from client to server offers XML's usual advantages: the data is self-describing and offers
the opportunity to loosely couple the client and server.
Sending XML to the client has another advantage. During the development cycle, you can use validating
XML parsers in emulated clients to ensure that the documents the server generates are clean. By the
time you try running the application on a real MIDP device, you'll be pretty sure that the data it's getting
is good.
The downside of XML is that it's not a very efficient way of expressing data. On slow wireless networks,
every byte counts. If you are considering XML as a data exchange format, do some testing with real
devices first to familiarize yourself with the delays involved. On today's wireless networks, latency is
usually more of an issue than data transfer rate, so you may notice the larger message size of XML
versus a binary format.
For another discussion of the use of XML in enterprise applications, see the Designing Wireless
Enterprise Applications Using JavaTM Technology white paper.
Parser Roundup
If there's a slogan for XML parsers in the MIDP world, it might be "Don't Supersize Me." Parsers are
traditionally bulky, featuring lots of code and hefty runtime memory requirements. In MIDP devices, the
memory available for code is usually small and individual applications may have a maximum code size.
The Motorola iDEN phones, for example, have an upper limit of 50 kB on the size of a MIDlet suite JAR
file. Aside from code size, the amount of memory available at runtime is also small. What you need is a
parser that's designed to be small and light.
Open source parsers are attractive because they give you lots of control. You can customize a parser if
you need additional features, and you can fix the parser if it has bugs.
There are three fundamental parser types. Which type you choose depends on how you want your
application to behave and what types of documents you're expecting to parse.
1. A model parser reads an entire document and creates a representation of the document in memory.
Model parsers use significantly more memory than other types of parsers.
2. A push parser reads through an entire document. As it encounters various parts of the document, it
notifies a listener object. (This is how the popular SAX API operates.)
3. A pull parser reads a little bit of a document at once. The application drives the parser through the
document by repeatedly requesting the next piece.
The following table summarizes the current offering of small XML parsers that are appropriate for MIDP.
Name License Size MIDP Type
ASXMLP 020308 Modified BSD 6 kB yes push, model
kXML 2.0 alpha EPL
9 kB yes pull
kXML 1.2 EPL 16 kB yes pull
MinML 1.7 BSD 14 kB no push
NanoXML 1.6.4 zlib/libpng
10 kB patch model
TinyXML 0.7 GPL 12 kB no model
Xparse-J 1.1 GPL 6 kB yes model
The Name and License columns contain links to the corresponding web pages and licenses. The Size
column indicates the size of the class files for the parser as contained in a JAR, which is an
approximation of how much size the parser will add to your MIDlet suite JAR. The MIDP column
indicates whether the parser will compile without modifications in a MIDP environment. Finally, the Type
column indicates the type of the parser, as discussed above.
Two parsers that did not make the list are NanoXML 2.2 Lite and XMLtp 1.7. Although both of these
parsers are small, they rely heavily on J2SE APIs and would require significant effort to port to MIDP.
file:///H|/20070611/web%20pages/Parsing%20XML%20in%20J2ME.htm (3 of 11)2007-6-12 21:27:30
剩余10页未读,继续阅读
资源评论
natureboy520
- 粉丝: 37
- 资源: 41
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功