Listening to Events

import javax.xml.stream.*;
import java.net.*;
import java.io.*;

 
public class EventLister {

  public static void main(String[] args) {
        
    if (args.length == 0) {
      System.err.println("Usage: java EventLister url" );
     return;    
    }
        
    try {

      InputStream in;
      try {
        URL u = new URL(args[0]);
        in = u.openStream();
      }
      catch (MalformedURLException ex) {
          // Maybe it's a file name
          in = new FileInputStream(args[0]);
      }

      XMLInputFactory factory = XMLInputFactory.newInstance();
      XMLStreamReader parser = factory.createXMLStreamReader(in);
        
      while (true) {
         int event = parser.next();
         if (event == XMLStreamConstants.START_ELEMENT) {
             System.out.println("Start tag");
         }
         else if (event == XMLStreamConstants.END_ELEMENT) {
             System.out.println("End tag");
         }
         else if (event == XMLStreamConstants.START_DOCUMENT) {
             System.out.println("Start document");
         }
         else if (event == XMLStreamConstants.CHARACTERS) {
             System.out.println("Text");
         }
         else if (event == XMLStreamConstants.CDATA) {
             System.out.println("CDATA Section");
         }
         else if (event == XMLStreamConstants.COMMENT) {
             System.out.println("Comment");
         }
         else if (event == XMLStreamConstants.DTD) {
             System.out.println("Document type declaration");
         }
         else if (event == XMLStreamConstants.ENTITY_REFERENCE) {
             System.out.println("Entity Reference");
         }
         else if (event == XMLStreamConstants.START_ENTITY) {
             System.out.println("Entity Reference");
         }
         else if (event == XMLStreamConstants.END_ENTITY) {
             System.out.println("Entity Reference");
         }
         else if (event == XMLStreamConstants.SPACE) {
             System.out.println("Ignorable white space");
         }
         else if (event == XMLStreamConstants.NOTATION_DECLARATION) {
             System.out.println("Notation Declaration");
         }
         else if (event == XMLStreamConstants.ENTITY_DECLARATION) {
             System.out.println("Entity Declaration");
         }
         else if (event == XMLStreamConstants.PROCESSING_INSTRUCTION) {
             System.out.println("Processing Instruction");
         }
         else if (event == XMLStreamConstants.END_DOCUMENT) {
             System.out.println("End Document");
             break;
         }
      }           
    }
    catch (XMLStreamException ex) {
       System.out.println(ex);  
    }
    catch (IOException ex) {
      System.out.println("IOException while parsing " + args[0]);   
    }
        
  }

}

Previous | Next | Top | Cafe con Leche

Copyright 2007 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified January 7, 2004