如何在黑莓中解析xml文档?

发布于 2024-08-25 10:18:05 字数 49 浏览 4 评论 0原文

如何在 Blackberry 中解析 xml 文件?我可以有链接或示例代码或教程吗?

How can I parse a xml file in Blackberry? Can I have a link or sample code or tutorial?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沧桑㈠ 2024-09-01 10:18:05

我使用 SAX 来处理来自 Web api 的 XML 响应,它对我来说效果很好。查看:http://developerlife.com/tutorials/?p=28

到底是什么你想用XML来完成吗?

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?

水波映月 2024-09-01 10:18:05

您应该有一个接口来实现侦听器,以便在解析结束后通知您的 UI 线程。

import java.util.Vector;

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

使用 MediaFeedListner 实现您的类,然后覆盖 mediaItemParsed(Vector mObject) 和异常(java.io.IOException ioe) 方法。

mediaItemParsed() 方法将具有通知 UI 线程并执行所需操作的逻辑。

这是 XML 解析器代码。

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("============================================");

        }
    }
}  

完成了。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文