-3

i am very new in blackberry, now i am trying to do XML programming, using defaulthandler in sax parser blackberry.

any sample code, url or any advice highly appreciated.

thanks

Regards..

jaleel
  • 1,169
  • 8
  • 22
  • 46
  • I think you should improve your question. Please be more specific. Use your favorite search tool, get some code and if you have problems use your favorite search tool again and then ask. There are many sax parser questions in SO where you can get code. – Juanma Baiutti Dec 02 '11 at 12:48

1 Answers1

0

Find the sample code here. Cheers. ;-)

// IBMRssXMLHandler.java
//
// MSI Services, Inc.
// Frank Ableson
// 973.448.0070
// fableson@msiservices.com
// code free to use for any purpose, commercial or otherwise

package com.msi.ibm.rssreader;

import org.xml.sax.helpers.*;
import org.xml.sax.*;
import java.lang.StringBuffer;

import com.msi.ibm.rssreader.IBMRssStorage.*;

/**
 * 
 */
class IBMRssXMLHandler extends DefaultHandler {
    StringBuffer sb = null;
    IBMRssFeed _feed = null;
    IBMRssItem item = null;
    boolean bStarted = false;

    IBMRssStorage rssStore = null;

    IBMRssXMLHandler(IBMRssFeed feed) {
        _feed = feed;
        rssStore = new IBMRssStorage();
    }

    public void warning(SAXParseException e) {
        System.err.println("warning: " + e.getMessage());
        bStarted = false;
    }

    public void error(SAXParseException e) {
        System.err.println("error: " + e.getMessage());
    }

    public void fatalError(SAXParseException e) {
        System.err.println("fatalError: " + e.getMessage());
        bStarted = false;
    }

    public void startDocument() throws SAXException {
        System.out.println("startDocument");
    }

    public void endDocument() throws SAXException {
        System.out.println("endDocument");
        // we've concluded this document, safe to create the feed.
        rssStore.closeStore();
    }

    public void startElement(String namespaceURI, String localName,
            String qName, Attributes atts) throws SAXException {
        System.out.println("startElement [" + localName + "]");// Attributes
                                                                // [" + atts.toString() + "]");
        sb = new StringBuffer("");
        if (localName.equals("item")) {
            bStarted = true;
            // new item, let's set up!
            item = rssStore.createItem();
        }
    }

    public void endElement(String namespaceURI, String localName, String qName)
            throws SAXException {
        System.out.println("endElement [" + localName + "] value = ["
                + sb.toString() + "]");

        if (bStarted == false)
            return;

        if (localName.equals("item")) {
            // store this item!
            item.setName(_feed.getName());
            System.out.println("Storing item! [" + item.toString());

            rssStore.addRecord(item);

        }
        if (localName.equals("title")) {
            item.setTitle(sb.toString());
        }

        if (localName.equals("link")) {
            item.setLink(sb.toString());
        }

        if (localName.equals("description")) {
            item.setDescription(sb.toString());
        }

        if (localName.equals("category")) {
            item.setCategory(sb.toString());
        }

        if (localName.equals("pubDate")) {
            item.setPubDate(sb.toString());
        }

        sb = new StringBuffer("");

    }

    public void characters(char ch[], int start, int length) {
        String theString = new String(ch, start, length);
        System.out.println("characters [" + theString + "]");
        sb.append(theString);
    }

}
Nilanchala
  • 5,891
  • 8
  • 42
  • 72