NekoRSSLister

import org.apache.xerces.xni.parser.XMLInputSource;
import org.apache.xerces.xni.XNIException;
import org.cyberneko.pull.*;
import org.cyberneko.pull.event.*;
import org.cyberneko.pull.parsers.Xerces2;
import java.io.IOException;

 
public class NekoRSSLister {

  public static void main(String[] args) {
        
    if (args.length == 0) {
      System.err.println("Usage: java NekoRSSLister url");
      return;   
    }
        
    try {
      XMLPullParser parser = new Xerces2();
      XMLInputSource source = new XMLInputSource(null, args[0], null);
      parser.setInputSource(source);
        
      XMLEvent event;
      boolean inTitle = false
      while ((event = parser.nextEvent()) != null) {
        switch (event.type) {
          case XMLEvent.ELEMENT: 
            ElementEvent element = (ElementEvent) event;
            String name = element.QName.localpart;
            if (name.equals("title") && element.QName.uri == null) {
                if (element.start) inTitle = true;
                else inTitle = false;
            }
            break;
          case XMLEvent.CHARACTERS: 
            if (inTitle) {
              CharactersEvent text = (CharactersEvent) event;
              System.out.println(text.text);
            }
            break;
          case XMLEvent.CDATA: 
            if (inTitle) {
              CDATAEvent text = (CDATAEvent) event;
              System.out.println(text.text);
            }
            break;
          default:
            // do nothing
        } 
      }
    }
    catch (XNIException ex) {
       System.out.println(args[0] + " is not well-formed"); 
       System.out.println(ex);  
    }
    catch (IOException ex) {
      System.out.println(args[0] + " could not be checked due to an " 
       + ex.getClass().getName());  
      ex.printStackTrace();      
    }
        
  }

}

Previous | Next | Top | Cafe con Leche

Copyright 2000-2003 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified November 16, 2002