Sie sind auf Seite 1von 69

Parsing XML in J2ME

Rohan Chandane
rohan.chandane@indiatimes.com
These notes are created by me, Rohan Chandane as learning material while pursuing MSc(CA) from SICSR 2005-2007 (CC)
Parse XML in Parse XML in
a MIDP client application
Multi-tier System Architecture
Typical multi-tier architecture
In web-centric systems
Clients - HTML browsers
Server - Serving HTML over HTTP & Server - Serving HTML over HTTP &
Applications which uses a database for
persistent storage.
Continued
Wireless
Client - Browser as front end application
Server - serving WML over WAP Server - serving WML over WAP
Standalone Client Approach
It can communicate in different ways
with server
HTTP connection HTTP connection
RMI as RPC
Any customized protocol
MIDP Client
Advantage
Provide a richer user interface
Run offline Run offline
Make updates to the server periodically
Disadvantage
Difficulty of client installation
Maintenance
MIDP Client
Has following limitations
Network connection setup is slow.
Data rates are slow. Data rates are slow.
The processor is slow.
Memory is scarce
Multi-tier System Architecture
with MIDP Client
A multi-tier architecture: with
use of XML
Server side code/application returns
data as XML document
No need to write custom server-side code No need to write custom server-side code
for each client type
One way to supporting multiple client types
XML document can be transformed using
XSLT into whatever client requirement. e.g.
HTML for desktop browses
WML for WAP browses
Continued
XML can be a data exchange format
XML can be send as it is
or
With some with simple transformation to send With some with simple transformation to send
more terse (brief and up to the point) XML
format.
Continued
Advantages of XML
Data is self-describing
Opportunity to loosely couple the client and
server server
loosely couple: even if they use incompatible system
technologies, can be joined together on demand to
create composite services, or disassembled just as
easily into their functional components
During development, XML parsing can be
validated using emulator, before running it on
application on MIDP device.
Continued
Disadvantages of XML
Not a very efficient way of expressing data
On slow wireless networks, before using XML
as a data exchange format, have to do some as a data exchange format, have to do some
real device testing.
(cause now a days latency is issue than
transfer rate)
Parser Overview
Small size phone need small size
application
Application and other required files should Application and other required files should
not exceed particular size limit for MIDP
device (normally 128kb)
Continued
Parser must be of small and light
Parser are traditionally bulky
Featuring lots of code. Featuring lots of code.
Hefty (large amount of) runtime memory
requirements.
Open source parsers are attractive
Can customize it if need additional features.
Can fix the parser if it has bugs.
Parser Types
3 Fundamental Types
A Model parser
A Push parser A Push parser
A Pull parser
Choosing a parser is depends on
Application behavior
Types of documents to be parsed
Continued
A model parser
Reads an entire document
Creates a representation of the document Creates a representation of the document
in memory
Use significantly more memory than other
types of parsers
This is how the popular DOM-based parser
operates
Continued
A push parser
Reads through an entire document.
As it encounters various parts of the As it encounters various parts of the
document, it notifies a listener object.
Use comparative much memory and
processing power
This is how the popular SAX API operates.
Continued
A pull parser
Reads a little bit of a document at once.
Parser goes through the document by Parser goes through the document by
repeatedly requesting the next piece.
Best suitable for J2ME application as take
comparatively less memory and processing
than other type of parser
XML Parser
An XML processing model describes the
steps an application should take to
process XML process XML
This model is called XML Parser
Java API for XML Processing (JAXP)
This is used to integrate an XML parser
into your Java applications
Mostly used with J2EE
Current offering of small XML
parsers for MIDP
Name License Size MlDP 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
Size: size of the class file MIDP: whether the parser will compile without modifications in a MIDP environment
Parsers represent the current offerings in the MIDP 1.0 world
Frequently used XML parsers
For resource-constrained devices
kXML
Written exclusively for the J2ME platform Written exclusively for the J2ME platform
(CLDC and MIDP).
NanoXML
Version 1.6.8 for MIDP, supports DOM parsing.
Incorporation a parser into
MIDlet suite
If parsers are .java files
Place these files into the src directory of
your J2MEWTK project your J2MEWTK project
If parsers are .jar or .zip archive of
.class files
Place the archive in the lib directory of the
J2MEWTK project.
Performance
MIDlet code run well in a constrained
environment
If XML parser used, code will become If XML parser used, code will become
significantly bigger and slower.
Code optimization can be the solution
The optimizations
Three Categories of optimizations
Runtime performance
User perception User perception
Deployment code size
1. Runtime performance
While designing XML document
Long time to set up a network connection
XML Document sent to a MIDlet should XML Document sent to a MIDlet should
contain useful information
Aggregate documents on the server side and
send one larger document.
Too large document will keep user waiting
for long time.
Continued
Find a balance between
Avoiding connection setup times
and and
Minimizing download wait times
2. User perception/experience
Do not lock-up UI While the MIDlet is
Parsing an XML document
Reading the document from the network Reading the document from the network
User can allow to perform offline tasks
while network activities
3. Deployment code size
Concern : size of your MIDlet suite JAR
Three Problems
Less space on MIDP device Less space on MIDP device
Downloading application over network is
slow
XML Parsing adds intensive string parsing,
which adds overhead in MIDP applications
Continued
Solution: Use of obfuscator (alter)
MIDlet suite JAR contains
Class files, images, icons & other resource files Class files, images, icons & other resource files
Obfuscator has following features
Removes unused classes
Removes unused methods and variables
Renames classes, packages, methods, and
variables
Continued
These features are fine and will reduce the
size of your MIDlet suite JAR
Example: if incorporated an XML parser in your
MIDlet project, there may be parts of the MIDlet project, there may be parts of the
parser that application never uses.
An obfuscator is good for pruning (cut back)
out the stuff don't need.
Use of obfuscator
Continued
To use obfuscator, it need proguard.jar file
to get it, do the following
Go to http://proguard.sourceforge.net/.
Select the "Download" link. Select the "Download" link.
Select "Download section".
Choose the latest version of proguard.zip and
save it to yours disk.
Extract the proguard.jar file from proguard.zip
into the bin subdirectory of the WTK.
ProGuard
What is ProGuard
A free Java class file shrinker, optimizer,
and obfuscator and obfuscator
It can detect and remove unused classes,
fields, methods, and attributes
It can then optimize bytecode and remove
unused instructions
Continued
It can rename the remaining classes,
fields, and methods using short
meaningless name
The resulting jars are smaller and harder to The resulting jars are smaller and harder to
reverse-engineer
Integrate ProGuard with WTK
ProGuard plug-in can be seamlessly
integrated in the Sun J2ME Wireless
Toolkit (WTK) Toolkit (WTK)
Edit in the file
{WTK_DIR}/wtklib/Linux/ktools.properties
or
{WTK_DIR}\wtklib\Windows\ktools.properti
es
Continued
Put these lines in above file (for windows)
obfuscator.runner.class.name: proguard.wtk.ProGuardObfuscator
obfuscator.runner.classpath: wtklib\\proguard3.6\\lib\\proguard.jar
Uncompress the zip file to
{WTK_DIR}\wtklib\proguard3.6
To apply it on projects
Open project in WTK
& go to
Project -> Package -> Create Obfuscated
Package
After using it
XML Parsing with kXML in
J2ME application J2ME application
Purpose
A J2ME application, ParseXML, that
parses XML documents
Displays the information encoded in Displays the information encoded in
those documents on a phone screen
Requirements
J2ME Wireless Toolkit 2.2
http://java.sun.com/products/sjwtoolkit/do
wnload-2_2.html
SDK of J2SE, J2EE SDK of J2SE, J2EE
kXML Parser 1.2
http://kxml.objectweb.org/software/downl
oads/
Any ASCII/Unicode Editor
Installation
Install J2SD & J2EE
Install WTK 2.2
Place kxml.zip file in following directory Place kxml.zip file in following directory
{WTK_DIR}/apps/{Project_Folder}/lib
XML file
<?xml version="1.0"?>
<testXML>
<fname>Rohan</fname>
<lname>Chandane</lname>
</testXML> </testXML>
This XML will be parsed by J2ME
applications and it will extract data from
tags <fname> & <lname>
Continued
XML data is made available MIDP
application (ParseXML) in the form of a
String, using any server side technology String, using any server side technology
Server side technology preferably
should be Java(J2EE), so it will be
better for integration
We can also use PHP, Ruby, ASP.net
instead
J2EE Application
Firstly, for generating XML we will
create J2EE application
We will use NetBeans 5.0 as J2EE IDE We will use NetBeans 5.0 as J2EE IDE
Follow the steps ahead -
File New Project
Select Category : Web
Select Project : Web Application
Project Name : J2EEXMLParsing
Select Location : As you want Press Finish
Double Click on Source Packages in Projects
Create new servlet : Right Click on Default Package, New Servlet
Write Class Name : GeneralXML Press Next >
Press Finish
Default Package will show GenerateXML.java
A new tab will show code of GenerateXML.java
Write a code for generating XML string as show in red square
Content type : text/xml , This will generate content of page as XML
Right Click in code window and Click Run File Menu
Press OK
Here is XML String generated and displayed in Browser
Address in red square need to call by MIDP application to retrive XML String
J2ME Application
Secondly, for reading XML we will
create J2ME application
We will use Wireless Toolkit 2.2 as We will use Wireless Toolkit 2.2 as
J2ME IDE & text editor
Follow the steps ahead -
Writing a Java code
We need to know
MIDP GUI programming
Use of javax.microedition.lcdui classes Use of javax.microedition.lcdui classes
J2ME network programming
Use of javax.microedition.io classes
Use of java.io classes
kXML XML parsing Programming
Use of org.kxml.parse classes
MIDP GUI programming
To display XML data, we require
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*; import javax.microedition.lcdui.*;
Class neet to extend MIDlet as a super
class
Use of classes
Display
TextBox
Continued
Here is the code
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class ParseXML extends MIDlet
{{
private Display display;
public ParseXML()
{
display = Display.getDisplay(this);
}
public void startApp()
{
TextBox t = new TextBox("XML Data","Here, will be displyed data extracted from XML", 1024, 0);
display.setCurrent(t);
}
public void pauseApp() { }
public void destroyApp(boolean unconditional) { }
}
J2ME network programming
To get XML data over network using
HTTP, we require
import javax.microedition.io classes import javax.microedition.io classes
import java.io classes
Use of classes
HttpConnection
Using Server URL, to get XML Stream -
http://localhost:8084/J2EEXMLParseing/GenerateXML
InputStreamReader
Continued
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
public class ParseXML extends MIDlet
{
private Display display;
private String url = "http://localhost:8084/J2EEXMLParseing/GenerateXML";
public ParseXML()
{
display = Display.getDisplay(this);
}
public void startApp()
{
try
{
downloadPage(url);
}
catch(IOException ioe)
{
System.out.println("Error : " + ioe);
}
}
public void pauseApp() {}
public void destroyApp(boolean unconditional) {}
private void downloadPage(String url) throws IOException
{
HttpConnection conn = (HttpConnection)Connector.open(url);
InputStreamReader doc = new InputStreamReader(conn.openInputStream());
}
}
kXML XML parsing
To use kXML parsing classes, methods
we require
import org.kxml classes import org.kxml classes
import org.kxml.parser classes
Use of classes
XmlParser
ParseEvent
Continued
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
import org.kxml.*;
import org.kxml.parser.*;
public class ParseXML extends MIDlet
{
private Display display;
private String url = "http://localhost:8084/J2EEXMLParseing/GenerateXML";
public ParseXML()
{
Check code below
{
display = Display.getDisplay(this);
}
public void startApp()
{
try
{
downloadPage(url);
}
catch(IOException ioe)
{
System.out.println("Error : " + ioe);
}
}
public void pauseApp() {}
public void destroyApp(boolean unconditional) {}
private void downloadPage(String url) throws IOException
{
HttpConnection conn = (HttpConnection)Connector.open(url);
InputStreamReader doc = new InputStreamReader(conn.openInputStream());
Code explanation
XmlParser parser = new XmlParser(doc);
This will create object of XmlParser and will
read InputStreamReader doc, XML stream read InputStreamReader doc, XML stream
which we got from HttpConnection from
server
ParseEvent event = parser.read();
This will create ParseEvent object event,
which will read all events from XmlParser
object parser
Continued
ParseEvent objects method
getType() - returns the event type integer
constant assigned to this event constant assigned to this event
These are possible event types:
Xml.START_TAG
Xml.END_TAG
Xml.TEXT
Xml.WHITESPACE
Xml.COMMENT
Xml.PROCESSING_INSTRUCTION
Xml.DOCTYPE
Xml.END_DOCUMENT
Continued
getName() - returns the (local) name of
the element started if instance of StartTag,
null otherwise
example - If element start tag is <fname>, example - If element start tag is <fname>,
then event.getName() it will return fname
Continued
getText() - returns a String, if event types
are
TEXT
PROCESSING_INSTRUCTION PROCESSING_INSTRUCTION
DOCTYPE
for rest events, it returns null
RSS: Rich Site Summary
Simple XML format that summarizes
headlines and story descriptions for a news
site site

Das könnte Ihnen auch gefallen