50RMB求SAX解析XML网站代码问题的解答
初学android,尝试做SAX解析网站XML,遇到问题,百思不得解答,希望高手不吝赐教,现将部分代码放出,如有兴趣,留下邮箱我把源文件传过去,修改后运行发回给我,QQ上简单交流得到正确答案,既帮您冲50元话费,或者VS VIP及其他等值商品,代码不算太难,花费不了太多时间,现将代码给出。主main:
package rss1.rss1;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class rss1 extends Activity{
/** Called when the activity is first created. */
private RSSFeed feed=null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
feed=getFeed1("http://2010.163.com/special/00863I1M/rss_2010.xml");
UpdateDisplay();
}
private RSSFeed getFeed1(String urlToRssFeed)
{
try
{
URL url=new URL(urlToRssFeed);
SAXParserFactory factory=SAXParserFactory.newInstance();
SAXParser parser=factory.newSAXParser();
XMLReader xmlreader=parser.getXMLReader();
RSSHandler theRssHandler=new RSSHandler();
xmlreader.setContentHandler(theRssHandler);
InputStream ips = url.openStream();
InputStreamReader ipsr= new InputStreamReader(ips, "utf-8");
InputSource is =new InputSource(ipsr);
xmlreader.parse(is);
RSSFeed mFeed = theRssHandler.getFeed();
RSSFeed mm = mFeed;
return mm;
}
catch (Exception ee)
{
return null;
}
}
private void UpdateDisplay()
{
TextView feedtitle=(TextView)findViewById(R.id.feedtitle);
TextView feedpubdate=(TextView)findViewById(R.id.feedpubdate);
ListView itemlist=(ListView)findViewById(R.id.itemlist);
if(feed==null)
{
feedtitle.setText("No RSS Feed Available");
return;
}
feedtitle.setText(feed.getTitle());
feedpubdate.setText(feed.getPubDate());
ArrayAdapter<RSSItem> adapter =new ArrayAdapter<RSSItem>(this,android.R.layout.易做图_list_item_1,feed.getAllItem());
itemlist.setAdapter(adapter);
itemlist.setSelection(0);
itemlist.setOnClickListener((OnClickListener) this);
}
public void onItemClickListener(AdapterView<?> parent,View v,int position,long id)
{
Intent itemintent = new Intent(rss1.this,ShowDescription.class);
Bundle b = new Bundle();
b.putString("title", feed.getItem(position).getTitle());
b.putString("description",feed.getItem(position).getDescription());
b.putString("link",feed.getItem(position).getLink());
b.putString("pubdate", feed.getItem(position).getPubDate());
itemintent.putExtra("android.intent.extra.INTENT", b);
this.startActivityForResult(itemintent,0);
}
}
RSSFeed:
public class RSSFeed {
private String _title = null;
private String _pubdate = null;
private int _itemcount = 0;
private List<RSSItem> _itemlist;
RSSFeed()
{
_itemlist=new Vector<RSSItem>(0);
}
int addItem(RSSItem item)
{
_itemlist.add(item);
_itemcount++;
return _itemcount;
}
RSSItem getItem(int location)
{
return _itemlist.get(location);
}
List<RSSItem> getAllItem()
{
return _itemlist;
}
int getItemCount()
{
return _itemcount;
}
void setTitle(String title)
{
_title=title;
}
void setPubDate(String pubdate)
{
_pubdate=pubdate;
}
String getTitle()
{
return _title;
}
String getPubDate()
{
return _pubdate;
}
}
RSSHandler:
public class RSSHandler extends DefaultHandler{
RSSFeed _feed;
RSSItem _item;
boolean bFoundChannel=false;
int currentstate=0;//不感兴趣则置为0
final int RSS_TITLE=1;
final int RSS_LINK=2;
final int RSS_DESCRIPTION=3;
final int RSS_CATEGORY=4;
final int RSS_PUBDATE=5;
int i=0;
RSSHandler()
{
}
RSSFeed getFeed()
{
return _feed;
}
//实现sax解析
public void startDocument() throws SAXException
{
_feed= new RSSFeed();
_item= new RSSItem();
}
public void endDocument() throws SAXException
{
}
public void startElement(String namespaceURI, String localName,String qName,
Attributes atts) throws SAXException
{
if(localName.equals("channel"))
{
/*bFoundChannel=true;*/
currentstate=0;
return;
}
/*if(localName.equals("image"))
{
bFoundChannel=true;
}*/
if(localName.equals("item"))
{
/*bFoundChannel=false;*/
_item=new RSSItem();
return;
}
if(localName.equals("title"))
{
currentstate=RSS_TITLE;
return;
}
/*if(localName.equals("title")&&bFoundChannel)
{
currentstate=6;
return;
}*/
if(localName.equals("description"))
{
currentstate=RSS_DESCRIPTION;
return;
}
if(localName.equals("link"))
{
currentstate=RSS_LINK;
return;
}
if(localName.equals("category"))
{
currentstate=RSS_CATEGORY;
return;
}
/*if(localName.equals("pubDate")&&bFoundChannel)
{
currentstate=7;
return;
}*/
if(localName.equals("pubDate"))
{
currentstate=RSS_PUBDATE;
}
currentstate=0;
}
public void endElement(String namespaceURI,String localName,String qName) throws SAXException
{
if(localName.equals("item"))
{
_feed.addItem(_item);
return;
}
}
public void characters(char ch[],int start,int length)
{
String theString=new String(ch,start,length);
switch(currentstate)
{
case RSS_TITLE:
_item.setTitle(theString);
_item.toString();
currentstate=0;
break;
case RSS_LINK:
_item.setLink(theString);
currentstate=0;
break;
case RSS_DESCRIPTION:
_item.setDescription(theString);
currentstate=0;
break;
case RSS_CATEGORY:
_item.setCategory(theString);
currentstate=0;
break;
case RSS_PUBDATE:
_item.setPubDate(theString);
currentstate=0;
break;
/*case 6:
_feed.setTitle(theString);
currentstate=0;
break;
case 7:
_feed.setPubDate(theString);
currentstate=0;
break;*/
case 0:
i=4;
break;
default:
i=0;
return;
}
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="rss1.rss1"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable = "true">
<activity android:name=".rss1"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest> --------------------编程问答-------------------- 发个含源码的RSS阅读器例子程序, http://puke365.gotoip3.com/mobile/rss.rar,下载后安装在模拟器或真机上可看到运行效果,点击"保存源码"可把源码保存在sdcard上 --------------------编程问答-------------------- 安装后应用名是:新浪3G新闻 --------------------编程问答-------------------- 我想看看。landry.sjm@gmail.com --------------------编程问答-------------------- LS的邮件已发,代码写的很乱,但主体部分不目标设置的断点怎么进不去StartElement函数里去,先谢谢LS的几位,问题得出,有物质感谢。
补充:移动开发 , Android