开发者

How to parse xml document in Blackberry?

开发者 https://www.devze.com 2022-12-23 00:07 出处:网络
How can I parse a xml file in Blackberry? Can开发者_如何转开发 I have a link or sample code or tutorial?I\'ve used SAX to process XML responses from a web api and it worked well for me. Check out: htt

How can I parse a xml file in Blackberry? Can开发者_如何转开发 I have a link or sample code or tutorial?


I've used SAX to process XML responses from a web api and it worked well for me. Check out: http://developerlife.com/tutorials/?p=28

What exactly are you trying to accomplish with XML?


You should have a interface to implement the listener in order to notify your UI thread once parsing is over.

import java.util.Vector;

public interface MediaFeedListner {           
          public void mediaItemParsed(Vector mObject);
          public void exception(java.io.IOException ioe);
}

implement your class with MediaFeedListner and then override the mediaItemParsed(Vector mObject) and exception(java.io.IOException ioe) methoods.

mediaItemParsed() method will have the logic for notifying the UI thread and perform required operations.

Here is the XML parser code.

package com.test.net;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Vector;
import net.rim.device.api.xml.parsers.ParserConfigurationException;
import net.rim.device.api.xml.parsers.SAXParser;
import net.rim.device.api.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import com.span.data.MediaObject;
import com.span.utils.FileManager;

public class MediaHandler extends DefaultHandler {
    protected static final String TAG_FEED = "feed";
    protected static final String TAG_ENTRY = "entry";
    protected static final String TAG_TITLE = "title";
    protected static final String TAG_MEDIA_GROUP = "group";
    protected static final String TAG_MEDIA_CATEGORY = "category";
    protected static final String TAG_MEDIA_CONTENT = "content";
    protected static final String TAG_MEDIA_DESCRIPTION = "description";
    protected static final String TAG_MEDIA_THUMBNAIL = "thumbnail";
    protected static final String ATTR_MEDIA_CONTENT= "url";
    protected static final String ATTR_MEDIA_THUMBNAIL = "url";

    boolean isEntry = false;
    boolean isTitle = false;
    boolean isCategory = false;
    boolean isDescription = false;
    boolean isThumbUrl = false;
    boolean isMediaUrl = false;
    boolean isMediaGroup = false;

    String valueTitle = "";
    String valueCategory = "";
    String valueDescription = "";
    String valueThumbnailUrl = "";
    String valueMediaUrl = "";

    public static Vector mediaObjects = null;

    MediaObject _dataObject =  null;
    MediaFeedListner listner = null;

    public MediaHandler(MediaFeedListner listner) {
        this.listner = listner;
        mediaObjects = new Vector();
    }

    public void parseXMLString(String xmlString) {
        try {
                SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
                parser.parse(new ByteArrayInputStream(xmlString.getBytes()), this);

        } 
        catch (ParserConfigurationException e) {
            e.printStackTrace();
        } 
        catch (SAXException e) {
            e.printStackTrace();
        } 
        catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {

        if(localName.equalsIgnoreCase(TAG_FEED)) {
            //return;
        }

        if(localName.equals(TAG_ENTRY))
        {
            _dataObject =  new MediaObject();
            isEntry = true;
        }

        if(isEntry) {

            if(localName.equalsIgnoreCase(TAG_TITLE)) {
                isTitle = true;
            }

            if(localName.equals(TAG_MEDIA_GROUP))
                isMediaGroup = true;

            if(isMediaGroup) {

                if(localName.equalsIgnoreCase(TAG_MEDIA_CONTENT)) {
                    valueMediaUrl = attributes.getValue(ATTR_MEDIA_CONTENT);
                    if(valueMediaUrl != null) {
                        _dataObject.setMediaUrl(valueMediaUrl);
                        valueMediaUrl = "";
                    }
                }

                if(localName.equalsIgnoreCase(TAG_MEDIA_THUMBNAIL)) {
                    valueThumbnailUrl = attributes.getValue(ATTR_MEDIA_THUMBNAIL);
                    if(valueThumbnailUrl != null) {
                        _dataObject.setMediaThumb(valueThumbnailUrl);
                        valueThumbnailUrl = "";
                    }
                }

                if(localName.equalsIgnoreCase(TAG_MEDIA_DESCRIPTION)) {
                    isDescription = true;
                }

                if(localName.equalsIgnoreCase(TAG_MEDIA_CATEGORY)) {
                    isCategory = true;
                }
            }
        }
    }

    public void characters(char[] ch, int start, int length) throws SAXException {

        if(isTitle){
            valueTitle = new String(ch, start, length);
            _dataObject.setMediaTitle(valueTitle);
            System.out.println("Title  value " + valueTitle);
            valueTitle = "";
        }

        if(isCategory){
            valueCategory = new String(ch, start, length);
            _dataObject.setMediaCategory(valueCategory);
            System.out.println("category  value " + valueCategory);
            valueCategory = "";
        }

        if(isDescription){
            valueDescription = new String(ch, start, length);
            _dataObject.setMediaDesc(valueDescription);
            System.out.println("category  value " + valueDescription);
            valueDescription = "";
        }
    }

    public void endElement(String uri, String localName, String name) throws SAXException {

        if(localName.equalsIgnoreCase(TAG_FEED)) {
            listner.mediaItemParsed(mediaObjects);
            printMediaInfo(mediaObjects);
        }
        if(localName.equalsIgnoreCase(TAG_ENTRY)) {
            isEntry = false;
            isTitle = false;
            isCategory = false;
            isDescription = false;

            mediaObjects.addElement(_dataObject);
        }
    }

    public static void printMediaInfo(Vector v){
        int length  = v.size();

        for(int i = 0 ; i <length ; i++){
            MediaObject mediaObj = (MediaObject) v.elementAt(i);

            FileManager.getInstance().writeLog("Title: " + mediaObj.getMediaTitle());
            FileManager.getInstance().writeLog("Category: " + mediaObj.getMediaCategory());
            FileManager.getInstance().writeLog("Desc: " + mediaObj.getMediaDesc());
            FileManager.getInstance().writeLog("URL: " + mediaObj.getMediaUrl());
            FileManager.getInstance().writeLog("Thumb: " + mediaObj.getMediaThumb());
            FileManager.getInstance().writeLog("Fav count: " + mediaObj.getMediaFavCount());
            FileManager.getInstance().writeLog("View Count: " + mediaObj.getMediaViewCount());
            FileManager.getInstance().writeLog("Ratings: " + mediaObj.getMediaRating());
            FileManager.getInstance().writeLog("============================================");

        }
    }
}  

Its done.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号