⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 soaprequest.java

📁 javaP2P技术内幕课程111213141516源代码
💻 JAVA
字号:
import org.w3c.dom.*;
import javax.xml.parsers.*;
import org.apache.crimson.tree.XmlDocument;
import java.io.*;


public class SOAPRequest {
    //Keeps reference of the complete XML document.
    private Document ownerDoc;

    //Keeps reference of the SOAP Envelope element.
    private Element soapEnvelope;

    //Keeps reference of the SOAP Body element.
    private Element soapBody;

    //We will author SOAPEnvelope
    //and an empty SOAP Body in the constructor.
    public SOAPRequest() {
        try {
            //Create a Document Builder Factory,
            //then create a Document Builder using the Factory,
            //then create a Document using the Builder.
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            ownerDoc = db.newDocument();
        } //try
        catch (ParserConfigurationException pce) {
            System.out.println("ParserConfigException:" + pce.getMessage());
        } //catch

        try {
            //Create the Envelope.
            soapEnvelope = ownerDoc.createElement("SOAP-ENV:Envelope ");


            //Set namespaces.
            soapEnvelope.setAttributeNS("http://www.w3.org/2000/xmlns/", 
                                        "xmlns:SOAP-ENV ", 
                                        "http://schemas.xmlsoap.org/soap/envelope/");
            soapEnvelope.setAttributeNS("http://www.w3.org/2000/xmlns/", 
                                        "xmlns:xsi ", 
                                        "http://www.w3.org/1999/XMLSchema-instance ");
            soapEnvelope.setAttributeNS("http://www.w3.org/2000/xmlns/", 
                                        "xmlns:xsd ", 
                                        "http://www.w3.org/1999/XMLSchema ");


            //Create an empty SOAP Body and
            //add it to the Envelope.
            soapBody = ownerDoc.createElement("SOAP-ENV:Body ");
            soapEnvelope.appendChild(soapBody);
            ownerDoc.appendChild(soapEnvelope);
        } //try
        catch (DOMException de) {
            System.out.println("DOMException:" + de.getMessage());
        } //catch
    } //Constructor

    public void setBodyMethod(Node bodyMethod) {
        //bodyMethod belongs to some other owner document.
        //We will import the Node into our document
        //and append it to the soapBody.
        Node importedNode = ownerDoc.importNode(bodyMethod, true);
        soapBody.appendChild(importedNode);

        //Now save the SOAP request XML as SOAPRequest.xml.
        //This saving is only for demonstration.
        XmlDocument xmlDocument = (XmlDocument) ownerDoc;

        try {
            FileOutputStream fout = new FileOutputStream(
                                            new File(".\\SoapRequest.xml "));
            xmlDocument.write(fout);
            fout.close();
        } //try
        catch (Throwable th) {
            th.printStackTrace();
        }
    } //setBodyMethod()

    //Main method only for demonstration.
    //Creates one node "method " with a few child elements..
    //Then it calls the setBodyMethod of SOAPRequest class
    //passing the "method " node as parameter..
    public static void main(String[] args) {
        SOAPRequest soap = new SOAPRequest();
        Document doc = null;
        Element method;
        Element parameter1;
        Element parameter2;
        Element parameter3;

        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            doc = db.newDocument();
        } //try
        catch (ParserConfigurationException pce) {
            System.out.println("ParserConfigException:" + pce.getMessage());
        } //catch

        try {
            method = doc.createElement("f:getFreight ");
            method.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:f ", 
                                  "www.freightservice.com/");
            parameter1 = doc.createElement("source ");
            parameter1.setAttribute("xsi:type ", "xsd:string ");
            parameter1.appendChild(doc.createTextNode("Lahore "));
            method.appendChild(parameter1);
            parameter2 = doc.createElement("destination ");
            parameter2.setAttribute("xsi:type ", "xsd:string ");
            parameter2.appendChild(doc.createTextNode("Multan "));
            method.appendChild(parameter2);
            parameter3 = doc.createElement("packetWeight ");
            parameter3.setAttribute("xsi:type ", "xsd:int ");
            parameter3.appendChild(doc.createTextNode("50 "));
            method.appendChild(parameter3);
            soap.setBodyMethod(method);
        } //try
        catch (DOMException de) {
            System.out.println("Method DOMException:" + de.getMessage());
        } //catch
    } //main
} //class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -