Meaningless Drivel is fun!
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
JavaRanch » Java Forums » Java » Web Services
Bookmark "dynamic generation of SOAP request using JAVA" Watch "dynamic generation of SOAP request using JAVA" New topic

dynamic generation of SOAP request using JAVA

mayank shar

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


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


// 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);

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");

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

// Close the SOAPConnection

catch (Exception e) {



// following SOAP request is obtained


<soapenv:Envelope xmlns:soapenv="" xmlns:xsd="" xmlns:xsi="">
<header soapenv:actor="" soapenv:mustUnderstand="0" xmlns="xmlapi_1.0">
<security xmlns="">
<user xmlns="">
<substitute property="username"/>
<password xmlns="">
<substitute property="password"/>
<requestID xmlns="">
<substitute property="requestId"/>
<netw.Network xmlns="xmlapi_1.0">
<instanceFullName xmlns="">network: <substitute property="ipAddress" xmlns=""/>
<command xmlns="">command name <substitute property="Id1" xmlns=""/>
text goes here <substitute property="id2" xmlns=""/>
Anurag Verma
Ranch Hand

Joined: Mar 30, 2012
Posts: 128

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:
subject: dynamic generation of SOAP request using JAVA
It's not a secret anymore!