import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.Node;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
public
class
Main {
public
void testSOAP(String opename,int x,int y){
String url =
"http://localhost:8089/index.php"
;
SOAPConnectionFactory soapConnectionFactory = null;
SOAPConnection soapConnection = null;
MessageFactory messageFactory = null;
SOAPMessage soapMessage = null;
SOAPPart soapPart = null;
SOAPEnvelope soapEnvelope = null;
SOAPBody body = null;
try
{
soapConnectionFactory = SOAPConnectionFactory.newInstance();
soapConnection = soapConnectionFactory.createConnection();
messageFactory = MessageFactory.newInstance();
soapMessage = messageFactory.createMessage();
soapPart = soapMessage.getSOAPPart();
soapEnvelope = soapPart.getEnvelope();
body = soapEnvelope.getBody();
SOAPElement element = body.addChildElement(soapEnvelope.createName(opename));
element.addChildElement(
"in0"
).addTextNode(String.valueOf(x));
element.addChildElement(
"in1"
).addTextNode(String.valueOf(y));
soapMessage.saveChanges();
SOAPMessage reply = soapConnection.call(soapMessage,url);
soapPart = reply.getSOAPPart();
soapEnvelope = soapPart.getEnvelope();
body = soapEnvelope.getBody();
Node returnvalue = (Node) body.getChildElements().next();
if
(returnvalue != null) {
if
(returnvalue.getChildNodes().item(0).getNodeName().equals(
"return"
)) {
List<hashmap>> ReturnArray =
new
ArrayList<hashmap>>();
for
(int i=0;i<returnvalue.getchildnodes string key=
"returnvalue.getChildNodes().item(0).getChildNodes().item(i).getNodeName();"
value=
"returnvalue.getChildNodes().item(0).getChildNodes().item(i).getNodeValue();"
system.out.println
else
returned ex ex.printstacktrace
try
soapconnection.close
catch
e todo auto-generated block e.printstacktrace
public
static
void main args main.testsoap>
<p><br>
运行结果如下:</p>
<p>3+4:<br>
#text:7<br>
9-7:<br>
#text:2<br>
20/4:<br>
#text:5</p>
<p
class
=
"item-note"
><br></p></returnvalue.getchildnodes></hashmap></hashmap>