Create SOAP message using Java

In this article, i am going to create the SOAP Message by using core Java Only. SOAP Stands for ” Simple Object Access Protocol”, which is used to exchange the structured information via Webservices.

SOAP Message consist of following three parts:

  1. SOAP-ENV:Envelope
  2. SOAP-ENV:Header
  3. SOAP-ENV:Body
SOAP Message Format for Web Services
SOAP Message Format for Web Services

To create the SOAP, first we will need to create the object of “javax.xml.soap.MessageFactory“, then create object of “javax.xml.soap.SOAPMessage“. This object of “SOAPMessage” will have all the messages inside it in “javax.xml.soap.SOAPEnvelope” object. Every “Envelope” will have the “Header” and “Body” as shown in below program:

package com.service.SOAPMain;


import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;

public class CreateSOAPMessage {

	 * @param args
	public static void main(String[] args) {
			MessageFactory factory = MessageFactory.newInstance();
			SOAPMessage soapMsg = factory.createMessage();
			SOAPPart part = soapMsg.getSOAPPart();

			SOAPEnvelope envelope = part.getEnvelope();
			SOAPHeader header = envelope.getHeader();
			SOAPBody body = envelope.getBody();

			header.addTextNode("Training Details");

			SOAPBodyElement element = body.addBodyElement(envelope.createName("JAVA", "training", ""));
			element.addChildElement("WS").addTextNode("Training on Web service");

			SOAPBodyElement element1 = body.addBodyElement(envelope.createName("JAVA", "training", ""));
			element1.addChildElement("Spring").addTextNode("Training on Spring 3.0");


			FileOutputStream fOut = new FileOutputStream("SoapMessage.xml");

			System.out.println("SOAP msg created");

		}catch(Exception e){



As the output, one xml file of named “SoapMessage.xml” will be created and also printed on the console.

SOAP Messsage Output - Java
SOAP Messsage Output – Java




Related Posts


5 responses to “Create SOAP message using Java”

  1. dhileepan Avatar

    can u please post for generate create soapFaualtClientException using

  2. Prakhar Vaibhav Avatar
    Prakhar Vaibhav

    Can you please tell how to add a parameter xmlns:ser with its value as a http url in SOAPEnvelop tag?

  3. Gary Avatar

    I am curious about the input name space and how it got its output as shivasoft

  4. Arati Avatar

    Great..I tried the same but it wont support in intellij . I am working on serialization java to xml i am using juneau library and i got the body part of xml but am not getting header part .Could you please suggest how to add header part from java pojos to xml binding using apache juneau library?Please

  5. […] far I was able to find this sample code here and tried to customize […]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Jitendra Zaa

Subscribe now to keep reading and get access to the full archive.

Continue Reading