80,337
社区成员
发帖
与我相关
我的任务
分享
package com.wo;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import android.util.Log;
public class MyDefaultHandler extends DefaultHandler {
String tagName = null;
static News news = null;
String type = null;
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
if (tagName != null) {
if (type != null && type.equals("News")) {
String data = new String(ch, start, length);
if (tagName.equals("ID")) {
news.setId(data);
Log.v("news id", data);
} else if (tagName.equals("TITLE")) {
news.setTitle(data);
Log.v("news title", data);
} else if (tagName.equals("CONTENT")) {
news.setContent(data);
Log.v("news content", data);
} else if (tagName.equals("PUBDATE")) {
news.setPubDate(data);
Log.v("news pubdate", data);
}
}
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (qName.equals("ITEMS")) {
if (type != null && type.equals("News")) {
Log.v("news", news.toString());
}
}
qName = null;
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if (qName.equals("ITEMS")) {
type = attributes.getValue("name");
if (type.equals("News")) {
news = new News();
} else if (type.equals("Single")) {
} else if (type.equals("House")) {
} else if (type.equals("Interview")) {[code=Java]
package com.wo;
public class News {
public String id;
public String title;
public String content;
public String pubDate;
public News() {
}
@Override
public String toString() {
return "News [id=" + id + ", title=" + title + ", content=" + content
+ ", pubDate=" + pubDate + "]";
}
/**
* @return the id
*/
public String getId() {
return id;
}
/**
* @param id
* the id to set
*/
public void setId(String id) {
this.id = id;
}
/**
* @return the title
*/
public String getTitle() {
return title;
}
/**
* @param title
* the title to set
*/
public void setTitle(String title) {
this.title = title;
}
/**
* @return the content
*/
public String getContent() {
return content;
}
/**
* @param content
* the content to set
*/
public void setContent(String content) {
this.content = content;
}
/**
* @return the pubDate
*/
public String getPubDate() {
return pubDate;
}
/**
* @param pubDate
* the pubDate to set
*/
public void setPubDate(String pubDate) {
this.pubDate = pubDate;
}
}
package com.wo;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import android.app.Activity;
import android.os.Bundle;
public class TestCursorActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DefaultHttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://www.10fang.com/api/data.xml");
try {
HttpResponse response = client.execute(request);
int code = response.getStatusLine().getStatusCode();
if (code == HttpStatus.SC_OK) {
InputStream is = response.getEntity().getContent();
InputStreamReader isr = new InputStreamReader(is, "utf-8");
InputSource inputSource = new InputSource(isr);
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
parser.parse(inputSource, new MyDefaultHandler());
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
{
Log.d("qName", qName);
@Override
public void characters(char[] ch, int start, int length) throws SAXException
{
Log.d("preTag ", preTag);
private List<News> newsList;
private News news;
private String preTag;
public List<News> getNewsList()
{
return newsList;
}
@Override
public void startDocument() throws SAXException
{
newsList = new ArrayList<News>();
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
{
//Log.e("qName", qName);
if ("ITEMS".equals(localName))
{
news = new News();
}
preTag = localName;
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException
{
if (news!=null)
{
String data = new String(ch, start,length);
if ("ID".equals(preTag))
{
news.setId(data);
}
else if ("TITLE".equals(preTag))
{
news.setTitle(data);
}
else if ("CONTENT".equals(preTag))
{
news.setContent(data);
}
else if ("PUBDATE".equals(preTag))
{
news.setPubDate(data);
}
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException
{
if ("ITEMS".equals(localName)&&news!=null)
{
newsList.add(news);
news = null;
}
preTag = null;
}
@Override
public void endDocument() throws SAXException
{
super.endDocument();
Log.e("endDocument", "");
}
<?xml version="1.0" encoding="utf-8" ?>
- <SOURCE>
- <ITEMS name="News">
<ID>53140</ID>
<TITLE>【赛程回顾】凤凰香郡摄影比赛</TITLE>
<CONTENT>“风景这边独好”2011凤凰香郡摄影大赛5月20日开始面向市民征稿,截止到5月31日征稿结束共收参赛作品579幅。6月12日,由凤凰香郡、十房网、摄影专业人士组成评审团,评选出33幅获奖作品。其中李军摄影作品“东城新画卷”荣获一等奖,奖金2000元,并被聘为《地产观》特约摄影师;李景成、赵航荣获二等奖,获得奖金800元;汪洋、李持等荣获三等奖,奖金300元;陈黎、杨钊、崔少卿等25人荣获优秀奖。</CONTENT>
<PUBDATE>2011-06-14</PUBDATE>
</ITEMS>
- <ITEMS name="News">
<ID>52990</ID>
<TITLE>2011年十堰房地产高峰论坛召开</TITLE>
<CONTENT>最严厉新政迄今一年有余,在一系列限购、限价措施下,一线城市成交量大跌,房价出现较大降幅,房企发展规划出现转折。那么在十堰,深受政策影响的房地产行业会将如何发展呢?</CONTENT>
<PUBDATE>2011-05-31</PUBDATE>
</ITEMS>
- <ITEMS name="News">
<ID>50625</ID>
<TITLE>十堰楼市2011 众楼盘抢滩1月群星荟萃</TITLE>
<CONTENT>虽说国家为了遏制不断上涨的房价,多次出台调控政策进行打压,但却未达到预期效果。而十堰楼市却是一片火热,没有出现楼市调控使房价降下来的局面。新年伊始,许多优质楼盘选择在一月打响新年开门红,今天特别针对2011年1月份即将开盘的楼盘做了集中展示,希望能给有意向置业者提供一些参考。</CONTENT>
<PUBDATE>2011-01-11</PUBDATE>
</ITEMS>
- <ITEMS name="News">
<ID>49936</ID>
<TITLE>楼市调查:房展会上来自开发商的声音</TITLE>
<CONTENT>大家可以看到,各个展位都聚满了购房者,置业顾问也在为他们耐心解答。那作为开发商,精心布置展位来参加这次房展会目的何在呢,我们一起去了解一下。</CONTENT>
<PUBDATE>2010-11-27</PUBDATE>
</ITEMS>
- <ITEMS name="News">
<ID>49878</ID>
<TITLE>调查:十堰房展会是楼市拐点吗(下)</TITLE>
<CONTENT>在上期节目中,我们从市民那里了解到,通过这次房展会,市民一致表示对房价下跌并不看好,观望气氛依然浓厚。那今天就让我们走进开发商,看看他们从这次房展会中又有什么样的收获呢?</CONTENT>
<PUBDATE>2010-11-24</PUBDATE>
</ITEMS>
</SOURCE>
public BuildFeed Read(Context context) {
try {
SAXParserFactory factory=SAXParserFactory.newInstance();
SAXParser parser=factory.newSAXParser();
XMLReader reader=parser.getXMLReader();
reader.setContentHandler(SaxReader.this);
InputSource is=new InputSource(context.getClassLoader().getResourceAsStream("building.xml"));
reader.parse(is);
return getFeed();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public BuildItem rItem;
public BuildFeed rFeed;
final int ID = 1;
final int NAME = 2;
final int ADDRESS = 3;
final int ABOUT = 4;
final int PUBDATE = 5;
private int currentstate = 0;
public BuildFeed getFeed(){
return rFeed;
}
@Override
public void startDocument() throws SAXException {
rFeed=new BuildFeed();
rItem=new BuildItem();
super.startDocument();
}
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
super.endDocument();
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
if(localName.endsWith("channel")){
currentstate = 0;
return;
}
if(localName.endsWith("item")){
rItem=new BuildItem();
return;
}
if(localName.equals("id")){
currentstate = ID;
return;
}
if(localName.equals("name")){
currentstate = NAME;
return;
}
if(localName.equals("address")){
currentstate = ADDRESS;
return;
}
if(localName.equals("about")){
currentstate = ABOUT;
return;
}
if(localName.equals("pubDate")){
currentstate = PUBDATE;
return;
}
currentstate = 0;
super.startElement(uri, localName, qName, attributes);
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if(localName.endsWith("item")){
rFeed.addItem(rItem);
}
super.endElement(uri, localName, qName);
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
super.characters(ch, start, length);
String theString = new String(ch, start, length);
switch(currentstate){
case ID:
rItem.setId(theString);
currentstate = 0;
break;
case NAME:
rItem.setName(theString);
currentstate = 0;
break;
case ADDRESS:
rItem.setAddress(theString);
currentstate = 0;
break;
case PUBDATE:
rItem.setPubdate(theString);
currentstate = 0;
break;
case ABOUT:
rItem.setAbout(theString);
currentstate = 0;
break;
default:
return;
}
}