// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.net.HttpURLConnection;
import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class XmlUploadDocument {
    private static void uploadDocument(String urlstr, Document doc) {
        try {
            URL url = new URL(urlstr);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("POST");
            con.setRequestProperty("fnm", "z2.xml");
            con.setDoOutput(true);
            OutputFormat fmt = new OutputFormat();
            fmt.setIndenting(true);
            fmt.setEncoding("ISO-8859-1");
            XMLSerializer ser = new XMLSerializer(con.getOutputStream(), fmt);
            ser.serialize(doc);
            con.connect();
            if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
                System.out.println("OK");
            } else {
                System.out.println("Error: " + con.getResponseMessage());
            }
            con.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) throws Exception {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.newDocument();
        Element all = doc.createElement("all");
        doc.appendChild(all);
        Element one1 = doc.createElement("one");
        one1.appendChild(doc.createTextNode("A"));
        all.appendChild(one1);
        Element one2 = doc.createElement("one");
        one2.appendChild(doc.createTextNode("BB"));
        all.appendChild(one2);
        Element one3 = doc.createElement("one");
        one3.appendChild(doc.createTextNode("CCC"));
        all.appendChild(one3);
        uploadDocument("http://localhost/uploaddoc.asp", doc);
    }
}
