[Android] ネットワークのRSSを拾ってパース

2011/10/16

こんにちは。きんくまです。
Androidの開発を調べています。
ネットワーク上のcnet.com RSSをひっぱってきて、
パースしたいです。

javaで配列を動的に扱うのにはArrayListが使えるみたい。

mainActivity.java

package com.demo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.app.Activity;
import android.os.Bundle;

public class HttpGetDemoActivity extends Activity {
	private ArrayList articles;
	private final String ITEM = "item";
	private final String TITLE = "title";
	private final String PUB_DATE = "pubDate";
	private final String GUID = "guid";
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        loadXML();
    }
    
    private void loadXML() {
        BufferedReader in = null;
        String xmlStr = "";
        try{
        	String url = "http://news.cnet.com/8300-1_3-1.xml?categoryId=1999&keywords=Gadget%20news";
        	HttpClient client = new DefaultHttpClient();
        	HttpGet request = new HttpGet(url);
        	HttpResponse responce = client.execute(request);
        	
        	in = new BufferedReader(new InputStreamReader(responce.getEntity().getContent()));
        	
        	StringBuffer sb = new StringBuffer("");
        	String line = "";
        	String NL = System.getProperty("line.separator");
        	while((line = in.readLine()) != null){
        		sb.append(line + NL);
        	}
        	in.close();
        	
        	xmlStr = sb.toString();
        	//System.out.println(page);
        	
        }catch(Exception e){
        	e.printStackTrace();
        }finally{
        	if(in != null){
        		try{
        			in.close();
        		}catch(IOException e){
        			e.printStackTrace();
        		}
        	}
        }
        
        try{
        	parseXML(xmlStr);
        }catch(IOException e){
        	e.printStackTrace();
        }catch(XmlPullParserException e){
        	e.printStackTrace();
        }
	}

	public void parseXML (String xmlStr) throws XmlPullParserException, IOException
    {
    	XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    	factory.setNamespaceAware(true);
    	XmlPullParser xpp = factory.newPullParser();
    	
    	xpp.setInput(new StringReader(xmlStr));
    	int eventType = xpp.getEventType();


    	Boolean isItem = false;
    	ArticleData articleData = null;
    	
    	while(eventType != XmlPullParser.END_DOCUMENT){
    		String xmlNodeName;
    		
    		switch(eventType){
    			case XmlPullParser.START_DOCUMENT:
    				articles = new ArrayList();
    				break;
    			case XmlPullParser.START_TAG:
    				xmlNodeName = xpp.getName();
    				if(xmlNodeName.equalsIgnoreCase(ITEM)){
    					isItem = true;
    					articleData = new ArticleData();
    				}
    				if(isItem){
        				if(xmlNodeName.equalsIgnoreCase(TITLE)){
        					articleData.title = xpp.nextText();
        				}else if(xmlNodeName.equalsIgnoreCase(PUB_DATE)){
        					articleData.pubDate = xpp.nextText();
        				}else if(xmlNodeName.equalsIgnoreCase(GUID)){
        					articleData.guid = xpp.nextText();
        				}
    				}
    				break;
    			case XmlPullParser.END_TAG:
    				xmlNodeName = xpp.getName();
    				if(xmlNodeName.equalsIgnoreCase(ITEM)){
    					isItem = false;
    					articles.add(articleData);
    				}
    				break;
    		}
    		
    		eventType = xpp.next();
    	}
    }
}

ArticleData.java

package com.demo;

public class ArticleData {
	String title;
	String pubDate;
	String guid;
}

XMLPullParseSample はこのサイトを参考にさせてもらいました。ありがとうございます。
>> Consuming A SOAP Web Service: XmlPullParser Alternative

LINEで送る
Pocket

自作iPhoneアプリ 好評発売中!
フォルメモ - シンプルなフォルダつきメモ帳
ジッピー電卓 - 消費税や割引もサクサク計算!

ページトップへ戻る