Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

dynamic generation of SOAP request using JAVA

mayank shar
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 167
Hibernate Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic