aspose file tools*
The moose likes Web Services and the fly likes dynamic generation of SOAP request using JAVA Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "dynamic generation of SOAP request using JAVA" Watch "dynamic generation of SOAP request using JAVA" New topic
Author

dynamic generation of SOAP request using JAVA

mayank shar
Greenhorn

Joined: May 28, 2012
Posts: 1
This is the code for dynamic generation of SOAP request (both header and body):




import java.net.URL;

import javax.xml.soap.MessageFactory;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;



public class CreateSoap {


public static void main(String[] args) {


try {
// Create a SOAPConnection
SOAPConnectionFactory factory = SOAPConnectionFactory.newInstance();

SOAPConnection connection = factory.createConnection();

// Create a SOAPMessage
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage message = messageFactory.createMessage();
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPHeader soapHeader = envelope.getHeader();
SOAPBody body = envelope.getBody();

//soapHeader.detachNode();
envelope.getNamespacePrefixes();

// Create a SOAPHeaderElement
SOAPHeaderElement headerElement = soapHeader.addHeaderElement(envelope.createName("header","","xmlapi_1.0"));

SOAPElement header1 = headerElement.addChildElement(envelope.createName("security"));

SOAPElement headerChild1 = header1.addChildElement(envelope.createName("user"));
SOAPElement headerGrantChild1 = headerChild1.addChildElement("substitute");
Name p1=envelope.createName("property");
headerGrantChild1.addAttribute(p1, "username");

SOAPElement headerChild2 = header1.addChildElement(envelope.createName("password"));
SOAPElement headerGrantChild2 = headerChild2.addChildElement("substitute");
Name p2=envelope.createName("property");
headerGrantChild2.addAttribute(p2, "password");

SOAPElement header2 = headerElement.addChildElement(envelope.createName("requestID"));
SOAPElement header2Child = header2.addChildElement("substitute");
Name p3=envelope.createName("property");
header2Child.addAttribute(p3, "requestId");

// Create a SOAPBodyElement
Name bodyName = envelope.createName("netw.Network", "", "xmlapi_1.0");
SOAPBodyElement bodyElement = body.addBodyElement(bodyName);

// Insert Content
Name name = envelope.createName("instanceFullName");
SOAPElement symbol = bodyElement.addChildElement(name);
symbol.addTextNode("network:");

Name prop = envelope.createName("substitute");
SOAPElement prop1 = symbol.addChildElement(prop);
Name property=envelope.createName("property");
prop1.addAttribute(property, "ipAddress");


Name name1 = envelope.createName("command");
SOAPElement symbol1= bodyElement.addChildElement(name1);
symbol1.addTextNode("command name");


Name prop2 = envelope.createName("substitute");
SOAPElement prop3 = symbol1.addChildElement(prop2);
Name property1=envelope.createName("property");
prop3.addAttribute(property1, "Id1");

symbol1.addTextNode("text goes here");

Name prop4= envelope.createName("substitute");
SOAPElement prop5 = symbol1.addChildElement(prop4);
Name property2=envelope.createName("property");
prop5.addAttribute(property2, "id2");


// Create an endpint point which is either URL or String type
URL endpoint = new URL("http://localhost:8080/CreateSoap");

// Check the input
System.out.println("\nSOAP REQUEST:\n");
message.writeTo(System.out);
System.out.println();

// Send a SOAPMessage (request) and then wait for SOAPMessage (response)
//SOAPMessage response = connection.call(message, endpoint);


// Close the SOAPConnection
connection.close();

}
catch (Exception e) {
e.printStackTrace();
}

}

}


// following SOAP request is obtained


SOAP REQUEST:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header>
<header soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next" soapenv:mustUnderstand="0" xmlns="xmlapi_1.0">
<security xmlns="">
<user xmlns="">
<substitute property="username"/>
</user>
<password xmlns="">
<substitute property="password"/>
</password>
</security>
<requestID xmlns="">
<substitute property="requestId"/>
</requestID>
</header>
</soapenv:Header>
<soapenv:Body>
<netw.Network xmlns="xmlapi_1.0">
<instanceFullName xmlns="">network: <substitute property="ipAddress" xmlns=""/>
</instanceFullName>
<command xmlns="">command name <substitute property="Id1" xmlns=""/>
text goes here <substitute property="id2" xmlns=""/>
</command>
</netw.Network></soapenv:Body></soapenv:Envelope>
Anurag Verma
Ranch Hand

Joined: Mar 30, 2012
Posts: 124

Welcome to CodeRanch mayank. this is not regarding the solution to your question, but a suggestion that start using code blocks for putting your code, this will keep the patience or readers alive to read your code & answer them.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: dynamic generation of SOAP request using JAVA