Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中的Soap请求_Java_Xml_Web Services_Soap - Fatal编程技术网

Java中的Soap请求

Java中的Soap请求,java,xml,web-services,soap,Java,Xml,Web Services,Soap,我需要用Java生成一个Soap请求。这是我需要生成和传递的xml文件: <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="website" xmlns:com="website/Common" xmlns:xm="http://www.w3.org/2005/05/xmlmime"> <soapenv:

我需要用Java生成一个Soap请求。这是我需要生成和传递的xml文件:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"     
   xmlns:ns="website"         
   xmlns:com="website/Common" 
   xmlns:xm="http://www.w3.org/2005/05/xmlmime">
   <soapenv:Header/>
   <soapenv:Body>
      <ns:RequestName>
         <ns:model>
            <ns:keys query="myquery;" ></ns:keys>
            <ns:instance></ns:instance>
         </ns:model>
      </ns:RequestName>
   </soapenv:Body>
</soapenv:Envelope>

我知道还有其他方法可以做到这一点,比如wsimport,但我想知道如何以这种方式做到这一点。我的意思是,在为Soap请求创建xml文件时,正确的Java语法是什么。下面是一些非常基本的语法:

 SOAPMessage message = messageFactory.createMessage();
 SOAPHeader header = message.getSOAPHeader();
 SOAPBody body = message.getSOAPBody();

 // Here is the XML it produces:
 <SOAP-ENV:Envelope
 xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
 <SOAP-ENV:Header/>
    <SOAP-ENV:Body>
    ...
    </SOAP-ENV:Body>
 </SOAP-ENV:Envelope>
SOAPMessage message=messageFactory.createMessage();
SOAPHeader=message.getSOAPHeader();
SOAPBody=message.getSOAPBody();
//以下是它生成的XML:
...

您可以尝试使用以下代码:

MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();

SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("ns", "website");
envelope.addNamespaceDeclaration("com", "website/Common");
envelope.addNamespaceDeclaration("xm", "http://www.w3.org/2005/05/xmlmime");
SOAPBody soapBody = envelope.getBody();
SOAPElement element = soapBody.addChildElement("RequestName", "ns");
SOAPElement modelElement = element.addChildElement("model", "ns");
SOAPElement soapElement = modelElement.addChildElement("keys", "ns");
soapElement.addAttribute(envelope.createName("query"), "myquery;");
modelElement.addChildElement("instance", "ns");

soapMessage.saveChanges();
soapMessage.writeTo(System.out);
这将产生以下输出:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:com="website/Common" 
    xmlns:ns="website" 
    xmlns:xm="http://www.w3.org/2005/05/xmlmime">
    <SOAP-ENV:Header/>
    <SOAP-ENV:Body>
        <ns:RequestName>
            <ns:model>
                <ns:keys query="myquery;"/>
                <ns:instance/>
            </ns:model>
        </ns:RequestName>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>


你说的“这种方式”是什么意思?从技术上讲,生成这种xml并将其流式传输是一种方式。您想到了什么?为Soap Reuqest生成此xml文件时,正确的Java语法是什么?