Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.xml.sax.Attributes;
- import org.xml.sax.ContentHandler;
- import org.xml.sax.Locator;
- import org.xml.sax.SAXException;
- import java.util.ArrayList;
- import java.util.Iterator;
- public class RSSContentHandler implements ContentHandler, Iterable<NewsItem> {
- public RSSContentHandler() {
- }
- private ArrayList<NewsItem> newsItems = new ArrayList<>();
- private String currentValue;
- private NewsItem item;
- @Override
- public Iterator<NewsItem> iterator() {
- return this.newsItems.iterator();
- }
- // Aktuelle Zeichen die gelesen werden, werden in eine Zwischenvariable
- // gespeichert
- public void characters(char[] ch, int start, int length) throws SAXException {
- currentValue = new String(ch, start, length);
- }
- // Methode wird aufgerufen wenn der Parser zu einem Start-Tag kommt
- public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
- if (localName.equals("item")) {
- this.item = new NewsItem();
- }
- }
- // Methode wird aufgerufen wenn der Parser zu einem End-Tag kommt
- public void endElement(String uri, String localName, String qName) throws SAXException {
- // System.out.println("localName: " + localName);
- // System.out.println("currentValue: " + currentValue);
- if (localName.equals("title") && item != null) {
- item.setTitle(currentValue);
- }
- if (localName.equals("link") && item != null) {
- item.setUrl(currentValue);
- }
- if (localName.equals("pubDate") && item != null) {
- item.setPublicationDate(currentValue);
- }
- if (localName.equals("description") && item != null) {
- item.setDescription(currentValue);
- }
- if (localName.equals("item")) {
- newsItems.add(item);
- // System.out.println(item);
- }
- }
- public void endDocument() throws SAXException {}
- public void endPrefixMapping(String prefix) throws SAXException {}
- public void ignorableWhitespace(char[] ch, int start, int length)
- throws SAXException {}
- public void processingInstruction(String target, String data)
- throws SAXException {}
- public void setDocumentLocator(Locator locator) { }
- public void skippedEntity(String name) throws SAXException {}
- public void startDocument() throws SAXException {}
- public void startPrefixMapping(String prefix, String uri)
- throws SAXException {}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement