Example: PullValidator

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

 
public class PullValidator {

  public static void main(String[] args) {
        
    if (args.length == 0) {
      System.err.println("Usage: java PullValidator url" );
     return;    
    }
        
    try {
      XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
      XmlPullParser parser = factory.newPullParser();
      try {
        parser.setFeature(XmlPullParser.FEATURE_VALIDATION, true);
      }
      catch (XmlPullParserException ex) {
         System.err.println("This is not a validating parser");   
         return;
      }

      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);
        
      for (int event = parser.next(); 
           event != XmlPullParser.END_DOCUMENT ;
            event = parser.next()) ;
            
      // If we get here there are no exceptions
      System.out.println(args[0] + " is valid");      
    }
    catch (XmlPullParserException ex) {
       System.out.println(args[0] + " is not valid");   
       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 6, 2002