getText() Example

import org.xmlpull.v1.*;
import java.net.*;
import java.io.*;

 
public class EventText {

  public static void main(String[] args) {
		
    if (args.length == 0) {
      System.err.println("Usage: java EventText url" );
	 return;	
    }
		
    try {
      XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
      factory.setNamespaceAware(true);
      XmlPullParser parser = factory.newPullParser();

      
      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]);
      }
      parser.setInput(in, null);
        
      while (true) {
  	     int event = parser.nextToken();
 	     if (event == XmlPullParser.START_TAG) {
             System.out.println("Start-tag: " + parser.getText()) ;
    	 }
         else if (event == XmlPullParser.END_TAG) {
             System.out.println("End-tag: " + parser.getText());
         }
         else if (event == XmlPullParser.START_DOCUMENT) {
             System.out.println("Start document: "  + parser.getText());
         }
         else if (event == XmlPullParser.TEXT) {
             System.out.println("Text: " + parser.getText());
         }
         else if (event == XmlPullParser.CDSECT) {
             System.out.println("CDATA Section: " + parser.getText());
         }
         else if (event == XmlPullParser.COMMENT) {
             System.out.println("Comment: " + parser.getText());
         }
         else if (event == XmlPullParser.DOCDECL) {
             System.out.println("Document type declaration: " + parser.getText());
         }
         else if (event == XmlPullParser.ENTITY_REF) {
             System.out.println("Entity Reference: " + parser.getText());
         }
         else if (event == XmlPullParser.IGNORABLE_WHITESPACE) {
             System.out.println("Ignorable white space: " + parser.getText());
         }
         else if (event == XmlPullParser.PROCESSING_INSTRUCTION) {
             System.out.println("Processing Instruction: " + parser.getText());
         }
  	     else if (event == XmlPullParser.END_DOCUMENT) {
             System.out.println("End Document: " + parser.getText());
             break;
         } // end else if
      }  // end while
    } // end try
    catch (XmlPullParserException ex) {
       System.out.println(ex);	
    }
    catch (IOException e) {
      System.out.println("IOException while parsing " + args[0]);	
    }
		
  }
 
}

Previous | Next | Top | Cafe con Leche

Copyright 2000-2002 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified September 11 2002