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

📄 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()

    // UDDI request authroing.
    public static void main(String[] args) {
        SOAPRequest soap = new SOAPRequest();
        Document doc = null;
        Element method;
        Element businessService;
        Element authInfo;
        Element name;
        Element description;
        Element categoryBag;
        Element keyedReference;

        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("save_service");
            method.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", 
                                  "urn:uddi-org:api_v2");
            method.setAttribute("generic", "2.0");
            authInfo = doc.createElement("authInfo");
            authInfo.appendChild(doc.createTextNode(
                                         "An authorization token string."));
            method.appendChild(authInfo);

            businessService = doc.createElement("businessService");
            businessService.setAttribute("serviceKey", "");
            businessService.setAttribute("businessKey", "F5E65...");
            method.appendChild(businessService);

            name = doc.createElement("name");
            name.appendChild(doc.createTextNode("The name of the service."));
            businessService.appendChild(name);

            description = doc.createElement("description");
            description.appendChild(doc.createTextNode(
                                            "Textual description of the Binding Template."));
            businessService.appendChild(description);

            categoryBag = doc.createElement("categoryBag");
            businessService.appendChild(categoryBag);

            keyedReference = doc.createElement("keyedReference");
            keyedReference.setAttribute("keyName", "UNSPSC");
            keyedReference.setAttribute("keyValue", "UNSPSC code");
            categoryBag.appendChild(keyedReference);

            doc.appendChild(method);

            // Now save the UDDI request as UDDIRequest.xml.
            XmlDocument xmlDocument = (XmlDocument) doc;

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

            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 + -