2012-08-01 276 views
0

我正在研究读取RSS源的ANDROID应用程序,所以我使用本教程(http://android-er.blogspot.com/2010/05/simple-rss-reader-ii-implement-with.html)/源代码在这里,并将其实现到我自己的url rss feeder。但是描述标签没有被显示出来......在馈线的xml中,几乎是cz,描述标签是CDATA。 我如何解析描述cdata在我的rss ??谢谢!CDATA未解析为ANDROID RSS阅读器

这是我的处理代码:

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 endDocument() throws SAXException { 
    // TODO Auto-generated method stub 
} 

@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 
    if (localName.equalsIgnoreCase("item")){ 
     feed.addItem(item); 
    } 
} 

@Override 
public void characters(char[] ch, int start, int length) 
     throws SAXException { 
    // TODO Auto-generated method stub 

    String strCharacters = new String(ch,start,length); 

    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); 
      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; 
} 


    } 

的RSSFeed:

import java.util.List; 
import java.util.Vector; 

public class RSSFeed { 
    private String title = null; 
    private String description = null; 
    private String link = null; 
    private String pubdate = null; 
    private List<RSSItem> itemList; 

RSSFeed(){ 
    itemList = new Vector<RSSItem>(0); 
} 

void addItem(RSSItem item){ 
    itemList.add(item); 
} 

RSSItem getItem(int location){ 
    return itemList.get(location); 
} 

List<RSSItem> getList(){ 
    return itemList; 
} 

void setTitle(String value) 
{ 
    title = value; 
} 
void setDescription(String value) 
{ 
    description = value; 
} 
void setLink(String value) 
{ 
    link = value; 
} 
void setPubdate(String value) 
{ 
    pubdate = value; 
} 

String getTitle() 
{ 
    return title; 
} 
String getDescription() 
{ 
    return description; 
} 
String getLink() 
{ 
    return link; 
} 
String getPubdate() 
{ 
    return pubdate; 
} 

    } 
+0

你能提供处理程序吗?我几天前完成了这个(可能使用相同的教程)并且它正在工作。 – Enrichman 2012-08-01 13:07:13

+0

我刚刚添加了我的处理程序代码:) – Beast 2012-08-01 13:18:24

+0

谢谢。尝试实施我的,做一些小的修改,以存储饲料参数。每次我看到一个Handler的时候,逻辑都在characters方法里面(应该在endElement之一中)。这是完全错误的。我应该在我的网站上做一个教程!大声笑 – Enrichman 2012-08-01 13:21:08

回答

1

这就是我所做的:

public class RssHandler extends DefaultHandler { 

private RssItem item; 
private RssFeed feed; 
private boolean inItem; 

private String value; 
private StringBuffer buffer; 

@Override 
public void startElement(
     String nameSpaceURI, 
     String localName, 
     String qName, 
     Attributes atts 
) { 

    buffer = new StringBuffer(); 

    if (localName.equals("channel")) { 
     feed = new RssFeed(); 
     inItem = false; 
    } else if (localName.equals("item")) { 
     item = new RssItem(); 
     inItem = true; 
    } 
} 

public void endElement(
     String uri, 
     String localName, 
     String qName) { 

    value = buffer.toString(); 
    buffer.setLength(0); 

    value = Html.fromHtml(value).toString(); 

    if (localName.equals("pubDate") && !inItem) { 
     feed.setPublishedDate(value); 
    } else if (localName.equals("title") && !inItem) { 
     feed.setTitle(value); 
    } else if (localName.equals("link") && !inItem) { 
     URL url = null; 
     try { 
      url = new URL(value); 
     } catch (MalformedURLException e) { 
      Log.e("ERR", "error while creating url from [" + value + "]"); 
     } 
     if (url != null) { 
      feed.setLink(url); 
     } 
    } else if (localName.equals("description") && !inItem) { 
     feed.setDescription(value); 
    } else if (localName.equals("pubDate") && inItem) { 
     item.setPublishedDate(value); 
    } else if (localName.equals("title") && inItem) { 
     item.setTitle(value); 
    } else if (localName.equals("link") && inItem) { 
     URL url = null; 
     try { 
      url = new URL(value); 
     } catch (MalformedURLException e) { 
      Log.e("ERR", "error while creating url from [" + value + "]"); 
     } 
     if (url != null) { 
      item.setLink(url); 
     } 
    } else if (localName.equals("description") && inItem) { 
     item.setDescription(value); 
    } else if (localName.equals("item")) { 
     feed.addItem(item); 
     inItem = false; 
    } 
} 

public void characters(char[] ch, int start, int end) { 
    buffer.append(new String(ch, start, end)); 
} 

public ArrayList<RssItem> getRss() { 
    return this.rss; 
} 
} 

希望这有助于。 :)

编辑:

我已经编辑我的答案,以配合您的饲料。应该管用。 如果一切正常,请不要忘记接受答案。 ;)

+0

sir你可以plz发送给我你的RSSReader类? – Beast 2012-08-05 22:39:00

+0

对不起,我正在度假!顺便说一句,我要让我的应用程序公开,如果你仍然需要它,我可以分享它。 :) – Enrichman 2012-08-13 10:43:12

+0

希望你玩得开心! :) ..我会喜欢它,如果你分享请!非常感谢您, – Beast 2012-08-14 01:01:42