在android RSS中解析CDATA

在android RSS中解析CDATA,android,rss,cdata,Android,Rss,Cdata,我正在尝试制作一个ANDROID应用程序来读取RSS提要,所以我使用了本教程()并将其实现到我自己的url RSS提要中。但是没有显示描述标记..在进料器的xml中主要是cz,描述标记是CDATA。如何解析rss中的cdata描述?谢谢! 这是我的RSSHandler代码: import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; publ

我正在尝试制作一个ANDROID应用程序来读取RSS提要,所以我使用了本教程()并将其实现到我自己的url RSS提要中。但是没有显示描述标记..在进料器的xml中主要是cz,描述标记是CDATA。如何解析rss中的cdata描述?谢谢! 这是我的RSSHandler代码:

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;
  }
这是我的RSSReader代码:

       public class AndroidRssReader extends ListActivity {

private RSSFeed myRssFeed = null;
//private ArrayList<RSSItem> myRssFeed = null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    try {
        //URL rssUrl = new URL("http://www.gov.hk/en/about/rss/govhkrss.data.xml");
        URL rssUrl = new URL("http://www.merehbi.com/online/index.php?option=com_content&view=category&layout=blog&id=58&Itemid=155&lang=ar&format=feed&type=rss");
        SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();
        SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
        XMLReader myXMLReader = mySAXParser.getXMLReader();
        RSSHandler myRSSHandler = new RSSHandler();
        myXMLReader.setContentHandler(myRSSHandler);
        InputSource myInputSource = new InputSource(rssUrl.openStream());
        myXMLReader.parse(myInputSource);

    myRssFeed = myRSSHandler.getFeed(); 
    } catch (MalformedURLException e) {
        e.printStackTrace();    
    } catch (ParserConfigurationException e) {
        e.printStackTrace();    
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();    
    }

    if (myRssFeed!=null)
    {
        TextView feedTitle = (TextView)findViewById(R.id.feedtitle);
        TextView feedDescribtion = (TextView)findViewById(R.id.feeddescribtion);
        TextView feedPubdate = (TextView)findViewById(R.id.feedpubdate);
        TextView feedLink = (TextView)findViewById(R.id.feedlink);
        feedTitle.setText(myRssFeed.getTitle());
        feedDescribtion.setText(myRssFeed.getDescription());
        feedPubdate.setText(myRssFeed.getPubdate());
        feedLink.setText(myRssFeed.getLink());

        ArrayAdapter<RSSItem> adapter =
                new ArrayAdapter<RSSItem>(this,
                        android.R.layout.simple_list_item_1,myRssFeed.getList());
        setListAdapter(adapter);    
    }   
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    Intent intent = new Intent(this,ShowDetails.class);
    Bundle bundle = new Bundle();
    bundle.putString("keyTitle", myRssFeed.getItem(position).getTitle());
    bundle.putString("keyDescription", myRssFeed.getItem(position).getDescription());
    bundle.putString("keyLink", myRssFeed.getItem(position).getLink());
    bundle.putString("keyPubdate", myRssFeed.getItem(position).getPubdate());
    intent.putExtras(bundle);
    startActivity(intent);  
}
    }
公共类AndroidsReader扩展了ListActivity{
私有RSSFeed myRssFeed=null;
//private ArrayList myRssFeed=null;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
试一试{
//URL rssUrl=新URL(“http://www.gov.hk/en/about/rss/govhkrss.data.xml");
URL rssUrl=新URL(“http://www.merehbi.com/online/index.php?option=com_content&view=category&layout=blog&id=58&Itemid=155&lang=ar&format=feed&type=rss");
SAXParserFactory mySAXParserFactory=SAXParserFactory.newInstance();
SAXParser mySAXParser=mySAXParserFactory.newSAXParser();
XMLReader myXMLReader=myaxParser.getXMLReader();
RSSHandler-myRSSHandler=新的RSSHandler();
setContentHandler(myRSSHandler);
InputSource myInputSource=新的InputSource(rssUrl.openStream());
parse(myInputSource);
myRssFeed=myRSSHandler.getFeed();
}捕获(格式错误){
e、 printStackTrace();
}捕获(ParserConfiguration异常e){
e、 printStackTrace();
}捕获(SAXE异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
如果(myRssFeed!=null)
{
TextView feedTitle=(TextView)findViewById(R.id.feedTitle);
TextView feedDescription=(TextView)findViewById(R.id.feedDescription);
TextView feedPubdate=(TextView)findViewById(R.id.feedPubdate);
TextView feedLink=(TextView)findViewById(R.id.feedLink);
feedTitle.setText(myRssFeed.getTitle());
feedDescription.setText(myRssFeed.getDescription());
feedPubdate.setText(myRssFeed.getPubdate());
feedLink.setText(myRssFeed.getLink());
阵列适配器=
新阵列适配器(此,
android.R.layout.simple_list_item_1,myRssFeed.getList();
setListAdapter(适配器);
}   
}
@凌驾
受保护的void onListItemClick(列表视图l、视图v、整数位置、长id){
Intent Intent=新Intent(这是ShowDetails.class);
Bundle=新Bundle();
bundle.putString(“keyTitle”,myRssFeed.getItem(position.getTitle());
bundle.putString(“keyDescription”,myRssFeed.getItem(position.getDescription());
bundle.putString(“keyLink”,myRssFeed.getItem(position.getLink());
bundle.putString(“keyPubdate”,myRssFeed.getItem(position.getPubdate());
意向。额外支出(捆绑);
星触觉(意向);
}
}

我使用本教程及其代码

并根据这个答案更改一些代码

publicstaticvoidmain(字符串[]args)引发异常{
File File=新文件(“data.xml”);
DocumentBuilder=DocumentBuilderFactory.newInstance().newDocumentBuilder();
文档doc=builder.parse(文件);
NodeList节点=doc.getElementsByTagName(“主题”);
对于(int i=0;i
我使用本教程及其代码

并根据这个答案更改一些代码

publicstaticvoidmain(字符串[]args)引发异常{
File File=新文件(“data.xml”);
DocumentBuilder=DocumentBuilderFactory.newInstance().newDocumentBuilder();
文档doc=builder.parse(文件);
NodeList节点=doc.getElementsByTagName(“主题”);
对于(int i=0;i
您有什么可行的解决方案吗?如果是,请帮助我。(3.6MB对于一个RSS源来说是相当大的。你有什么有效的解决方案吗?如果有,请帮助我。)(3.6MB对于一个RSS源来说是相当大的,顺便说一句。你的答案不完整你的答案不完整
      public static void main(String[] args) throws Exception {
    File file = new File("data.xml");
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = builder.parse(file);

    NodeList nodes = doc.getElementsByTagName("topic");
    for (int i = 0; i < nodes.getLength(); i++) {
      Element element = (Element) nodes.item(i);
      NodeList title = element.getElementsByTagName("title");
      Element line = (Element) title.item(0);
      System.out.println("Title: " + getCharacterDataFromElement(line));
    }
  }
  public static String getCharacterDataFromElement(Element e) {
    Node child = e.getFirstChild();
    if (child instanceof CharacterData) {
      CharacterData cd = (CharacterData) child;
      return cd.getData();
    }
    return "";
  }