Example 6-29: PostXML posts XML to any URL from the command line

import oracle.xml.parser.v2.*;
import java.net.*;
import org.xml.sax.*;
import XMLHttp;

public class PostXML {
  public static void main(String[] args) throws Exception {
    String filename = null,targetURL = null, proxy = null;
    for (int z=0;z < args.length; z++) {
      if (args[z].equals("-x")) {
        if (args.length > z + 1) proxy = args[++z];
        else errorExit("No proxy specified after -x option");
      }
      else if (filename  == null) filename  = args[z];
      else if (targetURL == null) targetURL = args[z];
    }
    if (filename != null && targetURL != null) {
      // If user supplied a proxy, set it
      if (proxy != null) XMLHttp.setProxy(proxy,"80");
      // Post the xml!
      PostXML px = new PostXML();
      px.post(filename,targetURL);
    }
    else errorExit("usage: PostXML [-x proxy] xmlfile targetURL");
  }
  // Post XML document in 'filename' to 'targetURL'
  public void post(String filename, String targetURL) {
    try {
      // Parse the file to be posted to make sure it's well-formed
      XMLDocument message = XMLHelper.parse(URLUtils.newURL(filename));
      // Construct the URL to make sure it's a valid URL
      URL target = new URL(targetURL);
      // Post the XML document to the target URL using XMLHttp.doPost
      XMLDocument response = XMLHttp.doPost(message,target);
      if (response == null) errorExit("Null response from service.");
      // If successful, print out the XMLDocument response to standard out
      else response.print(System.out);
    }
    // If the XML to post is ill-formed use XMLHelper to print err
    catch (SAXParseException spx) {errorExit(XMLHelper.formatParseError(spx));}
    // Otherwise, print out appropriate error messages
    catch (SAXException sx) { errorExit("Error parsing "+filename); }
    catch (MalformedURLException m){ errorExit("Error: "+targetURL+" invalid");}
    catch (Exception ex) { errorExit("Error: "+ex.getMessage()); }
  }
  private static void errorExit(String m){System.err.println(m);System.exit(1);}
}