Names Example

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

 
public class NamePrinter {

  public static void main(String[] args) {
        
    if (args.length == 0) {
      System.err.println("Usage: java NamePrinter 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: ");
             printEvent(parser);
         }
         else if (event == XMLStreamConstants.END_ELEMENT) {
             System.out.println("End tag");
             printEvent(parser);
         }
         else if (event == XMLStreamConstants.START_DOCUMENT) {
             System.out.println("Start document");
         }
         else if (event == XMLStreamConstants.CHARACTERS) {
             System.out.println("Text");
             printEvent(parser);
         }
         else if (event == XMLStreamConstants.CDATA) {
             System.out.println("CDATA Section");
             printEvent(parser);
         }
         else if (event == XMLStreamConstants.COMMENT) {
             System.out.println("Comment");
             printEvent(parser);
         }
         else if (event == XMLStreamConstants.DTD) {
             System.out.println("Document type declaration");
             printEvent(parser);
         }
         else if (event == XMLStreamConstants.ENTITY_REFERENCE) {
             System.out.println("Entity Reference");
             printEvent(parser);
         }
         else if (event == XMLStreamConstants.SPACE) {
             System.out.println("Ignorable white space");
             printEvent(parser);
         }
         else if (event == XMLStreamConstants.PROCESSING_INSTRUCTION) {
             System.out.println("Processing Instruction");
             printEvent(parser);
         }
         else if (event == XMLStreamConstants.END_DOCUMENT) {
             System.out.println("End Document");
             break;
         } // end else if
      }  // end while
    }
    catch (XMLStreamException ex) {
       System.out.println(ex);  
    }
    catch (IOException ex) {
      System.out.println("IOException while parsing " + args[0]);   
      ex.printStackTrace();
    }
        
  }
  
  private static void printEvent(XMLStreamReader parser) {
      String localName = parser.getLocalName();
      String prefix = parser.getPrefix();
      String uri = parser.getNamespaceURI();
      
      if (localName != null) System.out.println("\tLocal name: " + localName);
      if (prefix != null) System.out.println("\tPrefix: " + prefix);
      if (uri != null) System.out.println("\tNamespace URI: " + uri);
      System.out.println();
  }

}

Previous | Next | Top | Cafe con Leche

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