Pull Processing Instructions Example

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

 
public class PILister {

  public static void main(String[] args) {
        
    if (args.length == 0) {
      System.err.println("Usage: java PILister 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);
        
      for (int event = parser.next(); 
           parser.hasNext(); 
           event = parser.next()) {
         if (event == XMLStreamConstants.PROCESSING_INSTRUCTION) {
             String target = parser.getPITarget();
             String data = parser.getPIData();
             System.out.println("<?" + target + " " + data + "?>");
         }
      }  
      parser.close();
         
    }
    catch (XMLStreamException ex) {
       System.out.println(ex);  
    }
    catch (IOException e) {
      System.out.println("IOException while parsing " + args[0]);   
    }
        
  }

}

Previous | Next | Top | Cafe con Leche

Copyright 2000-2004 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified January 8, 2004