Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

JSR 172: XML Parsing in MIDP

3,133 views

Published on

Published in: Technology
  • Be the first to comment

JSR 172: XML Parsing in MIDP

  1. 1. JAXP Parsing in Java ME
  2. 2. JSR 172 <ul><li>JSR 172: Java ME Web Services </li></ul><ul><ul><li>Access to remote SOAP / XML Based web services </li></ul></ul><ul><ul><li>Parsing XML data </li></ul></ul>
  3. 3. JAXP Java ME Edition <ul><li>JSR 172 is a subset of JSR 063 </li></ul><ul><ul><li>JSR 63: Java API for XML Processing </li></ul></ul><ul><ul><li>http://jcp.org/en/jsr/detail?id=63 </li></ul></ul><ul><li>Requires only CLDC 1.0 </li></ul><ul><li>Basic XML Parsing capabilities </li></ul>
  4. 4. API Features and Restrictions <ul><li>SAX 2.0 subset </li></ul><ul><li>XML Namespaces </li></ul><ul><li>UTF-8 and UTF-16 support </li></ul><ul><li>May support validation (DTD) </li></ul><ul><li>No DOM – handling </li></ul><ul><ul><li>Too heavy, size and runtime memory </li></ul></ul><ul><li>No XSLT Transformations </li></ul>
  5. 5. Packages <ul><li>javax.xml.parsers </li></ul><ul><ul><li>To obtain and reference platform's parser </li></ul></ul><ul><ul><ul><li>SAXParser, SAXParserFactory... </li></ul></ul></ul><ul><li>org.xml.sax </li></ul><ul><ul><li>Subset of SAX 2.0 API Classes and interfaces. </li></ul></ul><ul><ul><ul><li>Attributes, Locator, InputSource, SAX??Exceptions </li></ul></ul></ul><ul><li>org.xml.sax.helpers </li></ul><ul><ul><li>Class to extend to receive parse events </li></ul></ul><ul><ul><ul><li>DefaultHandler </li></ul></ul></ul>
  6. 6. Basic Idea behind SAX <ul><li>SAX = Simple API for XML </li></ul><ul><ul><li>See http://en.wikipedia.org/wiki/Simple_API_for_XML </li></ul></ul><ul><li>Event driven way of handling xml-data </li></ul><ul><li>Only reading (no manipulation of the xml – data) </li></ul>
  7. 7. Creating Parser <ul><li>SAXParserFactory factory = SAXParserFactory.newInstance(); </li></ul><ul><li>SAXParser saxParser = factory.newSAXParser(); </li></ul><ul><li>saxParser. parse (..., ...); </li></ul>
  8. 8. SAXParser parse <ul><li>parse(org.xml.sax.InputSource is, DefaultHandler dh); </li></ul><ul><li>parse(java.io.InputStream is, DefaultHandler dh); </li></ul>
  9. 9. Default Handler? <ul><li>Create your own handler by inheriting the Default Handler </li></ul><ul><ul><li>Class MyHandler extends DefaultHandler { </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>Override methods you want </li></ul>
  10. 10. Example <ul><li>public class MyXMLParser extends DefaultHandler { </li></ul><ul><li>public void startDocument () throws SAXException { } </li></ul><ul><li>public void startElement (String uri, String localName, String qName, Attributes attributes) throws SAXException { } </li></ul><ul><li>public void characters (char[] ch, int start, int length) throws SAXException { } </li></ul><ul><li>public void endElement (String uri, String localName, String qName) throws SAXException { } </li></ul><ul><li>public void endDocument () throws SAXException { } </li></ul><ul><li>} </li></ul>
  11. 11. Creating Parser <ul><li>SAXParserFactory factory = SAXParserFactory.newInstance(); </li></ul><ul><li>SAXParser saxParser = factory.newSAXParser(); </li></ul><ul><li>String xml = &quot;<root><child>content</child></root>&quot;; </li></ul><ul><li>ByteArrayInputStream s = new ByteArrayInputStream(xml.getBytes()); </li></ul><ul><li>saxParser. parse (s, new MyXMLParser()); </li></ul>

×