jan

10

Posted by : admin | On : 10 janvier 2012

Rss Reader

Bellow a Simple code about how to make a simple Rss Reader handle by SAX handler.



package mapping.rss;

public class Item {
	private String title ;
	private String description;
	private String link;
	private String pubDate;
	private String source;
	private String mediaContent;
	private String mediaText;
	/**
	 * @return the title
	 */
	public String getTitle() {
		return title;
	}
	/**
	 * @param title the title to set
	 */
	public void setTitle(String title) {
		this.title = title;
	}
	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}
	/**
	 * @param description the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}
	/**
	 * @return the link
	 */
	public String getLink() {
		return link;
	}
	/**
	 * @param link the link to set
	 */
	public void setLink(String link) {
		this.link = link;
	}
	/**
	 * @return the pubDate
	 */
	public String getPubDate() {
		return pubDate;
	}
	/**
	 * @param pubDate the pubDate to set
	 */
	public void setPubDate(String pubDate) {
		this.pubDate = pubDate;
	}
	/**
	 * @return the source
	 */
	public String getSource() {
		return source;
	}
	/**
	 * @param source the source to set
	 */
	public void setSource(String source) {
		this.source = source;
	}
	/**
	 * @return the mediaContent
	 */
	public String getMediaContent() {
		return mediaContent;
	}
	/**
	 * @param mediaContent the mediaContent to set
	 */
	public void setMediaContent(String mediaContent) {
		this.mediaContent = mediaContent;
	}
	/**
	 * @return the mediaText
	 */
	public String getMediaText() {
		return mediaText;
	}
	/**
	 * @param mediaText the mediaText to set
	 */
	public void setMediaText(String mediaText) {
		this.mediaText = mediaText;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("Item [title=");
		builder.append(title);
		builder.append(", description=");
		builder.append(description);
		builder.append(", link=");
		builder.append(link);
		builder.append(", pubDate=");
		builder.append(pubDate);
		builder.append(", source=");
		builder.append(source);
		builder.append(", mediaContent=");
		builder.append(mediaContent);
		builder.append(", mediaText=");
		builder.append(mediaText);
		builder.append("]");
		return builder.toString();
	}

}

package xml;

import java.util.ArrayList;
import java.util.List;

import mapping.rss.Item;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SearchItemRssHandler extends DefaultHandler {
	private final static String CHANNEL="channel";
	private final static String ITEM="item";
	private final static String TITLE="title";
	private final static String LINK="link";
	private final static String DESCRIPTION="description";
	private final static String SOURCE="source";
	private final static String PUBDATE="PUBDATE";

	private boolean bfChannel = false;
	private boolean bfItem = false;
	private boolean bfTitle = false;
	private boolean bflink = false ;
	private boolean bfDescription = false ;
	private boolean bfSource = false ;
	private boolean bfPubdate = false; 

	private Item item ;
	private List<Item> items ;
	public void startElement(String uri, String localName,String qName,
			Attributes attributes) throws SAXException {
		if(qName.equalsIgnoreCase(CHANNEL)){
			items = new ArrayList<Item>();
			bfChannel = true;
		}
		if(qName.equalsIgnoreCase(ITEM)){
			item = new Item();
			bfItem = true ;
		}
		if(qName.equalsIgnoreCase(TITLE)){
			bfTitle = true ;
		}
		if(qName.equalsIgnoreCase(LINK)){
			bflink = true ;
		}
		if(qName.equalsIgnoreCase(PUBDATE)){
			bfPubdate = true ;
		}
		if(qName.equalsIgnoreCase(SOURCE)){
			bfSource = true ;
		}
		if(qName.equalsIgnoreCase(DESCRIPTION)){
			bfDescription = true ;
		}

	}
	public void endElement(String uri, String localName,
			String qName) throws SAXException {
		if(qName.equalsIgnoreCase(ITEM)){
			items.add(item);
			bfItem = false;
		}
		if(qName.equalsIgnoreCase(CHANNEL)){
			setItems(items);
			bfChannel = false ;
		}
	}
	public void characters(char ch[], int start, int length) throws SAXException {
		String resu= new String(ch, start, length);
		if(bfItem){
			bfItem = false;
		}
		if(bfDescription){
			if(item!=null)
			item.setDescription(resu);
			bfDescription = false;
		}
		if(bfTitle){
			if(item!=null)
			item.setTitle(resu);
			bfTitle = false;
		}
		if(bflink){
			if(item!=null)
			item.setLink(resu);
			bflink = false ;
		}
		if(bfPubdate){
			if(item!=null)
			item.setPubDate(resu);
			bfPubdate = false;
		}
		if(bfSource){
			if(item!=null)
			item.setSource(resu);
		}
	}
	/**
	 * @return the items
	 */
	public List<Item> getItems() {
		return items;
	}
	/**
	 * @param items the items to set
	 */
	public void setItems(List<Item> items) {
		this.items = items;
	}
}

///TEST Main 

package xml.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import mapping.rss.Item;

import org.xml.sax.InputSource;

import xml.SearchItemRssHandler;

public class ReadRssXmlFile {
	public static void main(String argv[]) {
		try {
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser saxParser = factory.newSAXParser();

			File file = new File("C:\\workspace\src\\rss-test-fr.xml");
			InputStream inputStream= new FileInputStream(file);
			Reader reader = new InputStreamReader(inputStream,"UTF-8");

			InputSource is = new InputSource(reader);
			is.setEncoding("UTF-8");

			SearchItemRssHandler handler = new SearchItemRssHandler();
			saxParser.parse(is, handler);
			List<Item> items  = handler.getItems();
			for (Item item : items) {
				System.out.println(item);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}