Example: PullDeclaration

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

 
public class PullDeclaration {

  public static void main(String[] args) {
        
    if (args.length == 0) {
      System.err.println("Usage: java PullDeclaration 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);
        
      boolean inItem = false;
      boolean inTitle = false;
      // I am relying on no recursion here. To fix this
      // just keep an int count rather than a boolean
      for (int event = XMLStreamConstants.START_DOCUMENT; 
           parser.hasNext(); 
           event = parser.next()) {
         if (event == XMLStreamConstants.START_DOCUMENT) {
             
           String encoding = parser.getCharacterEncodingScheme();
           if (encoding == null) encoding = parser.getEncoding();
           if (encoding == null) encoding = "UTF-8";
           String version = parser.getVersion();
           if (version == null) version = "1.0";
             
           String declaration = "<?xml version=\"";
           declaration += version;
           declaration += "\" encoding=\"";
           declaration += encoding;
           if (parser.standaloneSet()) {
             declaration += "\" standalone=\"";
             if (parser.isStandalone()) {
               declaration += "yes";
             }
             else {
               declaration += "no";                 
             }
           }
           declaration += "\"?>";
           System.out.println(declaration);
           break;
         }
      }  
      parser.close();
      
    }
    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 2000-2004 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified January 8, 2004