Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.supinfo.reader.sax;
- import java.util.ArrayList;
- import org.xml.sax.Attributes;
- import org.xml.sax.SAXException;
- import org.xml.sax.helpers.DefaultHandler;
- import android.net.Uri;
- import com.supinfo.reader.beans.RSSItem;
- public class RSSParser extends DefaultHandler {
- private boolean currenttag;
- private String currentvalue;
- private ArrayList<RSSItem> listitems;
- private RSSItem item;
- private String name;
- private boolean isChannel;
- public RSSParser() {
- currenttag = false;
- isChannel = false;
- listitems = new ArrayList<RSSItem>();
- }
- @Override
- public void startElement(String uri, String localName, String qName,
- Attributes attributes) throws SAXException {
- // TODO Auto-generated method stub
- super.startElement(uri, localName, qName, attributes);
- if (localName.equals("item")) {
- currenttag = true;
- item = new RSSItem();
- }
- else if (localName.equals("channel")) {
- isChannel = true;
- }
- }
- @Override
- public void characters(char[] ch, int start, int length)
- throws SAXException {
- // TODO Auto-generated method stub
- super.characters(ch, start, length);
- if (currenttag || isChannel) {
- currentvalue = new String(ch);
- }
- }
- @Override
- public void endElement(String uri, String localName, String qName)
- throws SAXException {
- // TODO Auto-generated method stub
- super.endElement(uri, localName, qName);
- if (currenttag && !currentvalue.isEmpty()) {
- if (localName.equals("title") && !isChannel) {
- item.setTitle(currentvalue);
- }
- else if (localName.equals("description")) {
- item.setDescription(currentvalue);
- }
- else if (localName.equals("link")) {
- item.setLink(Uri.parse(currentvalue));
- }
- else if (localName.equals("pubDate")) {
- item.setPublicationDate(currentvalue);
- }
- }
- else if (localName.equals("title") && isChannel) {
- name = currentvalue;
- isChannel = false;
- }
- if (localName.equals("item")) {
- listitems.add(item);
- currenttag = false;
- }
- }
- public ArrayList<RSSItem> getFlux() {
- return listitems;
- }
- public String getName() {
- return name;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement