package android.rss;
import android.app.Activity;
import android.os.Bundle;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import android.view.Menu;
import android.widget.TextView;
import java.net.URL;
import java.io.IOException;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.Attributes;
public class RSSFeed extends Activity {
/** Called when the activity is first created. */
String rssResult = "";
boolean item = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView rss = (TextView) findViewById(R.id.rss);
try {
URL rssUrl = new URL("http://rfs.ru/rss/news.rss");
// http://feeds2.feedburner.com/Mobilab
//http://go-football.org/news/rss.xml
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
XMLReader xmlReader = saxParser.getXMLReader();
RSSHandler rssHandler = new RSSHandler();
xmlReader.setContentHandler(rssHandler);
InputSource inputSource = new InputSource(rssUrl.openStream());
xmlReader.parse(inputSource);
rss.setText(rssResult);
} catch (IOException e) {rss.setText(e.getMessage());
} catch (SAXException e) {rss.setText(e.getMessage());
} catch (ParserConfigurationException e) {rss.setText(e.getMessage());
}
}
private class RSSHandler extends DefaultHandler {
public void startElement(String uri, String localName, String qName,
Attributes attrs) throws SAXException {
if (localName.equals("item"))
item = true;
if (!localName.equals("item") && item == true)
rssResult = rssResult + localName + ": " ;
}
public void endElement(String namespaceURI, String localName,
String qName) throws SAXException {
}
public void characters(char[] ch, int start, int length)
throws SAXException {
String cdata = new String(ch, start, length);
if (item == true)
rssResult = rssResult +(cdata.trim()).replaceAll("\\s+", " ")+"\t";
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
много тегов . хотелось видет текс и заголовок.без технич инф.