3

我正在尝试为 android 创建一个 rss 阅读器。所以我连接到一个 rss ulr 并获取一些 XML 信息。这是链接 - http://www.bulgarianhistory.org/feed/ 如果您打开它查看源代码,您会注意到有一个标签content:encoded。如何获取此标签中的信息?我的代码只是跳过它!我正在使用 SAX。这是我的 Parser 类:

public class RSSParser extends DefaultHandler {
    private final static String TAG_ITEM = "item";
    private final static String[] xmltags = { "title", "link", "pubDate", "description", "content" };

    private RSSItem currentitem = null;
    private ArrayList<RSSItem> itemarray = null;
    private int currentindex = -1;
    private boolean isParsing = false;
    private StringBuilder builder = new StringBuilder();

    public RSSParser(ArrayList<RSSItem> itemarray) {
        super();

        this.itemarray = itemarray;
    }

    @Override
        public void characters(char[] ch, int start, int length) throws SAXException {
            super.characters(ch, start, length);

            if(isParsing && -1 != currentindex && null != builder)
            {
                builder.append(ch,start,length);
            }
        }

    @Override
        public void startElement(String uri, String localName, String qName,Attributes attributes) throws SAXException {
            super.startElement(uri, localName, qName, attributes);

            if(localName.equalsIgnoreCase(TAG_ITEM))
            {
                currentitem = new RSSItem();
                currentindex = -1;
                isParsing = true;

                itemarray.add(currentitem);
            }
            else
            {
                currentindex = itemIndexFromString(localName);

                builder = null;

                if(-1 != currentindex)
                    builder = new StringBuilder();
            }
        }

    @Override
        public void endElement(String uri, String localName, String qName) throws SAXException {
            super.endElement(uri, localName, qName);

            if(localName.equalsIgnoreCase(TAG_ITEM))
            {
                isParsing = false;
            }
            else if(currentindex != -1)
            {
                if(isParsing)
                {
                    switch(currentindex)
                    {
                        case 0: currentitem.title = builder.toString();                 break;
                        case 1: currentitem.link = builder.toString();                  break;
                        case 2: currentitem.date = builder.toString();                  break;
                        case 3: currentitem.description= builder.toString();            break;
                        case 4: currentitem.content = builder.toString();               break;
                    }
                }
            }
        }

    private int itemIndexFromString(String tagname){
        int itemindex = -1;

        for(int index= 0; index<xmltags.length; ++index)
        {
            if(tagname.equalsIgnoreCase(xmltags[index]))
            {
                itemindex = index;

                break;
            }
        }

        return itemindex;
    }
}
4

2 回答 2

1

看起来带有命名空间的标签被您的if. 试试在这里打印标签名称

 private int itemIndexFromString(String tagname){
        int itemindex = -1;

        for(int index= 0; index<xmltags.length; ++index)
        {
            **PRINT (tagname)**
            if(tagname.equalsIgnoreCase(xmltags[index]))
            {

如果它打印为content:encoded,则需要修改xmltags []为 inlcudecontent:encoded而不是content

于 2012-10-25T16:34:38.430 回答
1

我真的建议不要Sax在 Android 上使用,因为据我记得它在 Android 上的性能真的很慢。您应该使用XmlPullParser附带的Android,因此无需添加任何外部jars,并且您将获得显着的性能改进。我刚刚测试过并将标签XmlPullParser解析<content:encoded>为任何其他标签,所以它会起作用。

于 2012-10-25T14:22:53.683 回答