在使用SAX解析XML时出错

时间:2011-11-23 05:28:20

标签: java android xml-parsing

<description>
SEBI : Decision taken by a listed investment company to dispose of a part of its
       investment is not “price sensitive information” within meaning of SEBI
      (Prohibition of Insider Trading) Regulations, 1992<br>;
      By <b>  [2011] 15 taxmann.com 229 (SAT)</b> 
</description>

这是xml我要在<br>之后解析数据。我能够在<br>之前解析,但在<br>

之后无法解析

这是我的句柄类代码:

package com.exercise;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class RSSHandler extends DefaultHandler {

    final int state_unknown = 0;
    final int state_title = 1;
    final int state_description = 2;
    final int state_link = 3;
    final int state_pubdate = 4;
    int currentState = state_unknown;

    RSSFeed feed;
    RSSItem item;

    boolean itemFound = false;

    RSSHandler(){
    }

    RSSFeed getFeed(){
        return feed;
    }

    @Override
    public void startDocument() throws SAXException {
        // TODO Auto-generated method stub
        feed = new RSSFeed();
        item = new RSSItem();

    }



    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        // TODO Auto-generated method stub

        if (localName.equalsIgnoreCase("item")){
            itemFound = true;
            item = new RSSItem();
            currentState = state_unknown;
        }
        else if (localName.equalsIgnoreCase("title")){
            currentState = state_title;
        }
        else if (localName.equalsIgnoreCase("description")){
            currentState = state_description;
        }
        else if (localName.equalsIgnoreCase("link")){
            currentState = state_link;
        }
        else if (localName.equalsIgnoreCase("pubdate")){
            currentState = state_pubdate;
        }
        else{
            currentState = state_unknown;
        }

    }


    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        // TODO Auto-generated method stub
        currentState = state_unknown;
        if (localName.equalsIgnoreCase("item")){
            feed.addItem(item);
        }


    }

    @Override
    public void characters(char ch[], int start, int length)
            throws SAXException {
        //super.characters(ch, start, length);
        // TODO Auto-generated method stub
        StringBuilder buf=new StringBuilder();


        if (buf!=null) {
            for (int i=start; i<start+length; i++) {
                buf.append(ch[i]);


            }

            String strCharacters=buf.toString();





                if (itemFound==true){
        // "item" tag found, it's item's parameter
            switch(currentState){
            case state_title:
                item.setTitle(strCharacters);
                break;
            case state_description:
                item.setDescription(strCharacters);  //here data coming
                break;
            case state_link:
                item.setLink(strCharacters);
                break;
            case state_pubdate:
                item.setPubdate(strCharacters);
                break;  
            default:
                break;
            }

        }

        else{
        // not "item" tag found, it's feed's parameter
            switch(currentState){
            case state_title:
                feed.setTitle(strCharacters);
                break;
            case state_description:
                feed.setDescription(strCharacters);
                break;
            case state_link:
                feed.setLink(strCharacters);
                break;
            case state_pubdate:
                feed.setPubdate(strCharacters);
                break;  
            default:
                break;
            }
        }

        currentState = state_unknown;
    }


}


}

4 个答案:

答案 0 :(得分:0)

&amp;是一个XML实体参考,意思是&amp;。

默认情况下,SAX会为您进行转换,因此如果您的源XML显示为hello&amp; goodbye,您应该看到hello&amp; goodbye。 浏览This链接。它可能解决你的问题

答案 1 :(得分:0)

您粘贴的第一个文字出了问题。 尝试在代码模式下再次发布XML(每行开头有4个空格)。

我怀疑你是用url编码格式的xml,并且在开始处理它之前你必须解码它。

答案 2 :(得分:0)

由于发布了XML无效,您可能还需要在文档中转义引号。

我不知道这是不是你的问题,但它会是一个贡献者。

(报价围绕“价格敏感信息”)

答案 3 :(得分:0)

我认为在您的情况下,问题是您正在characters()内初始化StringBuilder,因此每次都会创建新对象。不要在characters()中初始化它,而是尝试在startElement()

中对其进行初始化
@Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {

         StringBuilder buf=new StringBuilder()
..........
}