【问题标题】:How to user DefaultHandler class in BlackBerry如何在 BlackBerry 中使用 DefaultHandler 类
【发布时间】:2011-06-02 17:24:49
【问题描述】:

在 BlackBerry 应用程序中,我使用 SAX 解析器解析我的 XML。我知道如何使用 ContentHandler 接口进行 SAX 解析。但我想使用 DefaultHandler 类解析我的 XML 这样我就可以只提供我需要的那些方法的定义。 谁能给我一个示例代码,我如何使用 DefaultHandler 在 BlackBerry 中进行 SAX 解析的类

【问题讨论】:

    标签: sax global-asax


    【解决方案1】:

    对于xml之类的

    <items>
    <item>
      <title>title content </title>
       ..
    </item>
    ...
    </items>
    
    
    
    class XMLRSSHandler extends DefaultHandler {
    
        static final String TITLE = "title";
        static final String ITEM = "item";
    
        boolean isItem = false;
        boolean isTitle = false;
        Vector items = new Vector();
        String value = "";
    
        private Item currentItem = null;
    
        public XMLRSSHandler() {
        }
    
        public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
            if (name.equalsIgnoreCase(ITEM)) {
               currentItem = new Item();
            } else if (currentItem != null) {
                if (name.equalsIgnoreCase(TITLE)) {
                    isTitle = true;
                }
            }
        }
    
        public void characters(char[] ch, int start, int length) throws SAXException {
            if (isTitle) {
                value = new String(ch, start, length).trim();
            }
        }
    
        public void endElement(String uri, String localName, String name) throws SAXException {
            if (isTitle && name.equalsIgnoreCase(TITLE)) {
                isTitle = false;
                currentItem.setTitle(value);
                return;
            }
            if (currentAItem != null
                    && (name.equalsIgnoreCase(ITEM)) {
                Logger.debug("Loaded Item " + currentItem.getTitle());
                items.addElement(currentArticle);
                currentItem= null;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-07-12
      • 1970-01-01
      • 2013-03-26
      • 1970-01-01
      • 1970-01-01
      • 2014-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多