{"id":2691,"date":"2012-02-02T23:16:37","date_gmt":"2012-02-02T17:46:37","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=2691"},"modified":"2012-02-02T23:16:37","modified_gmt":"2012-02-02T17:46:37","slug":"create-soap-message-using-java","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/java\/create-soap-message-using-java\/","title":{"rendered":"Create SOAP message using Java"},"content":{"rendered":"<p>In this article, i am going to create the <a title=\"What is SOAP in Webservice\" href=\"http:\/\/en.wikipedia.org\/wiki\/SOAP\" target=\"_blank\">SOAP <\/a>Message by using core Java Only. SOAP Stands for &#8221; Simple Object Access Protocol&#8221;, which is used to exchange the structured information via <a title=\"What is Web Services\" href=\"http:\/\/en.wikipedia.org\/wiki\/Web_Service\" target=\"_blank\">Webservices<\/a>.<\/p>\n<p>SOAP Message consist of following three parts:<\/p>\n<ol>\n<li>SOAP-ENV:Envelope<\/li>\n<li>SOAP-ENV:Header<\/li>\n<li>SOAP-ENV:Body<\/li>\n<\/ol>\n<figure id=\"attachment_2694\" aria-describedby=\"caption-attachment-2694\" style=\"width: 220px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/02\/SOAP.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2694\" title=\"SOAP Message Format for Web Services\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/02\/SOAP.png?resize=220%2C235&#038;ssl=1\" alt=\"SOAP Message Format for Web Services\" width=\"220\" height=\"235\" \/><\/a><figcaption id=\"caption-attachment-2694\" class=\"wp-caption-text\">SOAP Message Format for Web Services<\/figcaption><\/figure>\n<p><!--more-->To create the SOAP, first we will need to create the object of &#8220;<strong>javax.xml.soap.MessageFactory<\/strong>&#8220;, then create object of &#8220;<strong>javax.xml.soap.SOAPMessage<\/strong>&#8220;. This object of &#8220;SOAPMessage&#8221; will have all the messages inside it in &#8220;<strong>javax.xml.soap.SOAPEnvelope<\/strong>&#8221; object. Every &#8220;Envelope&#8221; will have the &#8220;Header&#8221; and &#8220;Body&#8221; as shown in below program:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npackage com.service.SOAPMain;\n\nimport java.io.FileOutputStream;\n\nimport javax.xml.soap.MessageFactory;\nimport javax.xml.soap.SOAPBody;\nimport javax.xml.soap.SOAPBodyElement;\nimport javax.xml.soap.SOAPEnvelope;\nimport javax.xml.soap.SOAPHeader;\nimport javax.xml.soap.SOAPMessage;\nimport javax.xml.soap.SOAPPart;\n\npublic class CreateSOAPMessage {\n\n\t\/**\n\t * @param args\n\t *\/\n\tpublic static void main(String&#x5B;] args) {\n\t\ttry{\n\t\t\tMessageFactory factory = MessageFactory.newInstance();\n\t\t\tSOAPMessage soapMsg = factory.createMessage();\n\t\t\tSOAPPart part = soapMsg.getSOAPPart();\n\n\t\t\tSOAPEnvelope envelope = part.getEnvelope();\n\t\t\tSOAPHeader header = envelope.getHeader();\n\t\t\tSOAPBody body = envelope.getBody();\n\n\t\t\theader.addTextNode(&quot;Training Details&quot;);\n\n\t\t\tSOAPBodyElement element = body.addBodyElement(envelope.createName(&quot;JAVA&quot;, &quot;training&quot;, &quot;https:\/\/jitendrazaa.com\/blog&quot;));\n\t\t\telement.addChildElement(&quot;WS&quot;).addTextNode(&quot;Training on Web service&quot;);\n\n\t\t\tSOAPBodyElement element1 = body.addBodyElement(envelope.createName(&quot;JAVA&quot;, &quot;training&quot;, &quot;https:\/\/jitendrazaa.com\/blog&quot;));\n\t\t\telement1.addChildElement(&quot;Spring&quot;).addTextNode(&quot;Training on Spring 3.0&quot;);\n\n\t\t\tsoapMsg.writeTo(System.out);\n\n\t\t\tFileOutputStream fOut = new FileOutputStream(&quot;SoapMessage.xml&quot;);\n\t\t\tsoapMsg.writeTo(fOut);\n\n\t\t\tSystem.out.println();\n\t\t\tSystem.out.println(&quot;SOAP msg created&quot;);\n\n\t\t}catch(Exception e){\n\t\t\te.printStackTrace();\n\t\t}\n\n\t}\n\n}\n<\/pre>\n<p>As the output, one xml file of named &#8220;SoapMessage.xml&#8221; will be created and also printed on the console.<\/p>\n<figure id=\"attachment_2695\" aria-describedby=\"caption-attachment-2695\" style=\"width: 576px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/02\/SOAP-Messsage-Output-Java.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2695\" title=\"SOAP Messsage Output - Java\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/02\/SOAP-Messsage-Output-Java.png?resize=576%2C173&#038;ssl=1\" alt=\"SOAP Messsage Output - Java\" width=\"576\" height=\"173\" \/><\/a><figcaption id=\"caption-attachment-2695\" class=\"wp-caption-text\">SOAP Messsage Output - Java<\/figcaption><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Step by Step Example of creating SOAP Message using core Java API<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[3],"tags":[329,216,227],"class_list":["post-2691","post","type-post","status-publish","format-standard","hentry","category-java","tag-java","tag-web-service","tag-xml"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":3773,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-faq-part-19\/","url_meta":{"origin":2691,"position":0},"title":"Salesforce interview questions &#8211; Part 19","author":"Jitendra","date":"December 16, 2014","format":false,"excerpt":"Salesforce interview questions for developers and admins around Apex, Visualforce, getting Salesforce object name on basis of Id, Apex API limits","rel":"","context":"In &quot;Apex&quot;","block_context":{"text":"Apex","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/apex\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3299,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/create-simple-mass-delete-button-for-listview-using-javascript\/","url_meta":{"origin":2691,"position":1},"title":"Create Simple Mass Delete Button for ListView using Salesforce Ajax Toolkit","author":"Jitendra","date":"May 6, 2013","format":false,"excerpt":"In Many situation, I needed a Mass Delete Kind of button. Where, i can select records in ListView and simply click \"Delete\" button. In this example, we will create a simple List View button and add on \"Search Layout\" of that object. So, create a new Custom Button with following\u2026","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Salesforce Mass Delete Button - List View","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/05\/Salesforce-Mass-Delete-Button-List-View.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/05\/Salesforce-Mass-Delete-Button-List-View.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/05\/Salesforce-Mass-Delete-Button-List-View.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":4501,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/using-soapui-with-salesforce-to-test-standard-and-custom-web-services-response\/","url_meta":{"origin":2691,"position":2},"title":"Using soapUI with Salesforce to test standard and custom web services response","author":"Jitendra","date":"May 27, 2015","format":false,"excerpt":"soapUI is most common tool available to test Soap based web services, it also has capability to test REST web services. soapUI can be used to test Partner WSDL, enterprise WSDL, Tooling API, Metadata API to study capability and response from Salesforce before writing any code in Java, C# or\u2026","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Create New SoapUI project","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/05\/Create-New-SoapUI-project.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/05\/Create-New-SoapUI-project.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/05\/Create-New-SoapUI-project.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":7041,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-load-testing-using-soapui\/","url_meta":{"origin":2691,"position":3},"title":"Salesforce Load Testing using SOAPUI","author":"Jitendra","date":"April 23, 2020","format":false,"excerpt":"Previously, I have added multiple posts in this blog on how you would face governor limit errors on each step of Salesforce implementation and how we can leverage selenium to perform load testing of Salesforce to some extent. SOAP UI is powerful tool and in this post we will see\u2026","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"API Load Testing using SOAPUI","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/04\/API-Load-Testing-using-SOAPUI.png?fit=1029%2C529&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/04\/API-Load-Testing-using-SOAPUI.png?fit=1029%2C529&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/04\/API-Load-Testing-using-SOAPUI.png?fit=1029%2C529&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2020\/04\/API-Load-Testing-using-SOAPUI.png?fit=1029%2C529&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":3537,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/consuming-external-webservice-in-apex\/","url_meta":{"origin":2691,"position":4},"title":"Consuming External Web Service in Apex &#8211; Salesforce","author":"Jitendra","date":"October 13, 2013","format":false,"excerpt":"One of the feature we have in Salesforce is that we can easily consume External Web Services. In this article, we will learn step by step demo of consuming Web Service in Apex. There are many public websites available to consume Web Service and one of them, I am using\u2026","rel":"","context":"In &quot;Apex&quot;","block_context":{"text":"Apex","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/apex\/"},"img":{"alt_text":"Generating Apex from WSDL in Salesforce","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/10\/Generating-Apex-from-WSDL-in-Salesforce-1024x345.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/10\/Generating-Apex-from-WSDL-in-Salesforce-1024x345.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/10\/Generating-Apex-from-WSDL-in-Salesforce-1024x345.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":3411,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/apex-interview-question-salesforce-part-16\/","url_meta":{"origin":2691,"position":5},"title":"Apex Interview Question \u2013 Salesforce &#8211; Part 16","author":"Jitendra","date":"July 28, 2013","format":false,"excerpt":"151. Give Sample Code Snippet of Apex that that will show that how Parent and Child record can be inserted in Single Statement ? Ans : It can be done with help of External Id. [java] Date dt = Date.today().addDays(7); Opportunity newOpportunity = new Opportunity(Name = 'shivasoft', StageName = 'Prospecting',\u2026","rel":"","context":"In &quot;Apex&quot;","block_context":{"text":"Apex","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/apex\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/2691","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=2691"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/2691\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=2691"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=2691"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=2691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}