Parsing XML in J2ME

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    6 Favorites

    Parsing XML in J2ME - Presentation Transcript

    1. 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)
    2. Parse XML in a MIDP client application
    3. Multi-tier System Architecture
    4. Typical multi-tier architecture In web-centric systems Clients - HTML browsers Server - Serving HTML over HTTP & Applications which uses a database for persistent storage.
    5. Continued… Wireless Client - Browser as front end application Server - serving WML over WAP
    6. Standalone Client Approach It can communicate in different ways with server HTTP connection RMI as RPC Any customized protocol
    7. MIDP Client Advantage Provide a richer user interface Run offline Make updates to the server periodically Disadvantage Difficulty of client installation Maintenance
    8. MIDP Client Has following limitations Network connection setup is slow. Data rates are slow. The processor is slow. Memory is scarce
    9. Multi-tier System Architecture with MIDP Client
    10. 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 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
    11. Continued… XML can be a data exchange format XML can be send as it is or With some with simple transformation to send more terse (brief and up to the point) XML format.
    12. Continued… Advantages of XML Data is self-describing Opportunity to loosely couple the client and 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.
    13. 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 real device testing. (cause now a days latency is issue than transfer rate)
    14. Parser Overview Small size phone need small size application Application and other required files should not exceed particular size limit for MIDP device (normally 128kb)
    15. Continued… Parser must be of small and light Parser are traditionally bulky 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.
    16. Parser Types 3 Fundamental Types A Model parser A Push parser A Pull parser Choosing a parser is depends on Application behavior Types of documents to be parsed
    17. Continued… A model parser Reads an entire 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
    18. Continued… A push parser Reads through an entire document. 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.
    19. Continued… A pull parser Reads a little bit of a document at once. 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
    20. XML Parser An XML processing model describes the steps an application should take to 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
    21. Current offering of small XML parsers for MIDP 1DPH /LFHQVH 6L]H 0,'3 7\\SH SXVK ASXMLP 020308 Modified BSD  N% \\HV PRGHO kXML 2.0 alpha EPL  N% \\HV SXOO kXML 1.2 EPL  N% \\HV SXOO MinML 1.7 BSD  N% QR SXVK NanoXML 1.6.4 zlib/libpng  N% SDWFK PRGHO TinyXML 0.7 GPL  N% QR PRGHO Xparse-J 1.1 GPL  N% \\HV PRGHO 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
    22. Frequently used XML parsers For resource-constrained devices kXML Written exclusively for the J2ME platform (CLDC and MIDP). NanoXML Version 1.6.8 for MIDP, supports DOM parsing.
    23. Incorporation a parser into MIDlet suite If parsers are .java files Place these files into the src directory of your J2MEWTK project If parsers are .jar or .zip archive of .class files Place the archive in the lib directory of the J2MEWTK project.
    24. Performance MIDlet code run well in a constrained environment If XML parser used, code will become significantly bigger and slower. Code optimization can be the solution
    25. The optimizations Three Categories of optimizations Runtime performance User perception Deployment code size
    26. 1. Runtime performance While designing XML document Long time to set up a network connection 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.
    27. Continued… Find a balance between Avoiding connection setup times and Minimizing download wait times
    28. 2. User perception/experience Do not lock-up UI While the MIDlet is Parsing an XML document Reading the document from the network User can allow to perform offline tasks while network activities
    29. 3. Deployment code size Concern : size of your MIDlet suite JAR Three Problems Less space on MIDP device Downloading application over network is slow XML Parsing adds intensive string parsing, which adds overhead in MIDP applications
    30. Continued… Solution: Use of obfuscator (alter) MIDlet suite JAR contains 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
    31. 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 parser that application never uses. An obfuscator is good for pruning (cut back) out the stuff don't need.
    32. Use of obfuscator
    33. 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 \"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.
    34. ProGuard What is ProGuard A free Java class file shrinker, optimizer, and obfuscator It can detect and remove unused classes, fields, methods, and attributes It can then optimize bytecode and remove unused instructions
    35. Continued… It can rename the remaining classes, fields, and methods using short meaningless name The resulting jars are smaller and harder to reverse-engineer
    36. Integrate ProGuard with WTK ProGuard plug-in can be seamlessly integrated in the Sun J2ME Wireless Toolkit (WTK) Edit in the file {WTK_DIR}/wtklib/Linux/ktools.properties or {WTK_DIR}\\wtklib\\Windows\\ktools.properti es
    37. 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
    38. After using it…
    39. XML Parsing with kXML in J2ME application
    40. Purpose A J2ME application, ParseXML, that parses XML documents Displays the information encoded in those documents on a phone screen
    41. Requirements J2ME Wireless Toolkit 2.2 http://java.sun.com/products/sjwtoolkit/do wnload-2_2.html SDK of J2SE, J2EE kXML Parser 1.2 http://kxml.objectweb.org/software/downl oads/ Any ASCII/Unicode Editor
    42. Installation Install J2SD & J2EE Install WTK 2.2 Place kxml.zip file in following directory {WTK_DIR}/apps/{Project_Folder}/lib
    43. XML file <?xml version=\"1.0\"?> <testXML> <fname>Rohan</fname> <lname>Chandane</lname> </testXML> This XML will be parsed by J2ME applications and it will extract data from tags <fname> & <lname>
    44. Continued… XML data is made available MIDP application (ParseXML) in the form of a 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
    45. J2EE Application Firstly, for generating XML we will create J2EE application We will use NetBeans 5.0 as J2EE IDE Follow the steps ahead -
    46. Select Category : Web File New Project… Select Project : Web Application
    47. Project Name : J2EEXMLParsing Select Location : As you want Press Finish
    48. Double Click on Source Packages in Projects Create new servlet : Right Click on Default Package, New Servlet…
    49. Write Class Name : GeneralXML Press Next >
    50. Press Finish
    51. Default Package will show GenerateXML.java A new tab will show code of GenerateXML.java
    52. Write a code for generating XML string as show in red square Content type : text/xml , This will generate content of page as XML
    53. Right Click in code window and Click Run File Menu
    54. Press OK
    55. Here is XML String generated and displayed in Browser Address in red square need to call by MIDP application to retrive XML String
    56. J2ME Application Secondly, for reading XML we will create J2ME application We will use Wireless Toolkit 2.2 as J2ME IDE & text editor Follow the steps ahead -
    57. Writing a Java code We need to know MIDP GUI programming 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
    58. MIDP GUI programming To display XML data, we require import javax.microedition.midlet.*; import javax.microedition.lcdui.*; Class neet to extend ‘MIDlet’ as a super class Use of classes Display TextBox
    59. 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) { } }
    60. J2ME network programming To get XML data over network using HTTP, we require 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
    61. 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()); } }
    62. kXML XML parsing To use kXML parsing classes, methods we require – import org.kxml classes import org.kxml.parser classes Use of classes XmlParser ParseEvent
    63. Continued… Check code below 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() { 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());
    64. Code explanation XmlParser parser = new XmlParser(doc); This will create object of XmlParser and will 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
    65. Continued… ParseEvent object’s method getType() - returns the event type integer 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
    66. Continued… getName() - returns the (local) name of the element started if instance of StartTag, null otherwise example - If element start tag is <fname>, then event.getName() it will return fname
    67. Continued… getText() - returns a String, if event types are TEXT PROCESSING_INSTRUCTION DOCTYPE for rest events, it returns null
    68. RSS: Rich Site Summary Simple XML format that summarizes headlines and story descriptions for a news site

    + Rohan ChandaneRohan Chandane, 12 months ago

    custom

    6095 views, 6 favs, 3 embeds more stats

    Parsing XML in J2ME

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 6095
      • 6073 on SlideShare
      • 22 from embeds
    • Comments 0
    • Favorites 6
    • Downloads 56
    Most viewed embeds
    • 19 views on http://mycodefactory.blogspot.com
    • 2 views on http://static.slideshare.net
    • 1 views on http://ig.gmodules.com

    more

    All embeds
    • 19 views on http://mycodefactory.blogspot.com
    • 2 views on http://static.slideshare.net
    • 1 views on http://ig.gmodules.com

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories