RandomGuy32

Aufgabe D3: RSSContentHandler

Nov 29th, 2019
166
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import org.xml.sax.Attributes;
  2. import org.xml.sax.ContentHandler;
  3. import org.xml.sax.Locator;
  4. import org.xml.sax.SAXException;
  5.  
  6. import java.util.ArrayList;
  7. import java.util.Iterator;
  8.  
  9. public class RSSContentHandler implements ContentHandler, Iterable<NewsItem> {
  10.     public RSSContentHandler() {
  11.  
  12.     }
  13.  
  14.     private ArrayList<NewsItem> newsItems = new ArrayList<>();
  15.     private String currentValue;
  16.     private NewsItem item;
  17.  
  18.     @Override
  19.     public Iterator<NewsItem> iterator() {
  20.         return this.newsItems.iterator();
  21.     }
  22.  
  23.     // Aktuelle Zeichen die gelesen werden, werden in eine Zwischenvariable
  24.     // gespeichert
  25.     public void characters(char[] ch, int start, int length) throws SAXException {
  26.         currentValue = new String(ch, start, length);
  27.     }
  28.  
  29.     // Methode wird aufgerufen wenn der Parser zu einem Start-Tag kommt
  30.     public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
  31.         if (localName.equals("item")) {
  32.             this.item = new NewsItem();
  33.         }
  34.     }
  35.  
  36.     // Methode wird aufgerufen wenn der Parser zu einem End-Tag kommt
  37.     public void endElement(String uri, String localName, String qName) throws SAXException {
  38.         // System.out.println("localName: " + localName);
  39.         // System.out.println("currentValue: " + currentValue);
  40.  
  41.         if (localName.equals("title") && item != null) {
  42.             item.setTitle(currentValue);
  43.         }
  44.  
  45.         if (localName.equals("link") && item != null) {
  46.             item.setUrl(currentValue);
  47.         }
  48.  
  49.         if (localName.equals("pubDate") && item != null) {
  50.             item.setPublicationDate(currentValue);
  51.         }
  52.  
  53.         if (localName.equals("description") && item != null) {
  54.             item.setDescription(currentValue);
  55.         }
  56.  
  57.         if (localName.equals("item")) {
  58.             newsItems.add(item);
  59.             // System.out.println(item);
  60.         }
  61.     }
  62.  
  63.     public void endDocument() throws SAXException {}
  64.     public void endPrefixMapping(String prefix) throws SAXException {}
  65.     public void ignorableWhitespace(char[] ch, int start, int length)
  66.             throws SAXException {}
  67.     public void processingInstruction(String target, String data)
  68.             throws SAXException {}
  69.     public void setDocumentLocator(Locator locator) {  }
  70.     public void skippedEntity(String name) throws SAXException {}
  71.     public void startDocument() throws SAXException {}
  72.     public void startPrefixMapping(String prefix, String uri)
  73.             throws SAXException {}
  74. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×