Populating the SOAP Body with Request XML Elements

This section describes steps to populate the SOAP body with Web service request XML elements. envelope.createName() and addBodyElement() methods are needed.

In the previous section, we learned how to create a new empty SOAPMessage object and how to access the SOAP body in the object. Now need to learn how create and add our Web service XML elements in the SOAP body.

As an example, let's try to populate the SOAP body with our "GetSpeech" Web service XML element as described at http://www.xmlme.com/WSShakespeare.asmx?op=GetSpeech:

   <GetSpeech xmlns="http://xmlme.com/WebServices">

1. Assuming "request" is the new SOAPMessage object, we need to get hold of the SOAP envelope object and SOAP body object first:

   SOAPEnvelope envelope = request.getSOAPPart().getEnvelope();
   SOAPBody body = envelope.getBody();

2. Use envelope.createName() to create javax.xml.soap.Name objects to represent the GetSpeech service and parameter element names with the correct namespace:

   Name serviceName = envelope.createName("GetSpeech", prefix, uri);
   Name paramName = envelope.createName("Request", prefix, uri);

3. Create and add a body element to the body with the body.addBodyElement() method:

   SOAPBodyElement service = body.addBodyElement(serviceName);

4. Create and add a child element to the body element with the service.addChildElement() method:

   SOAPBodyElement param = service.addChildElement(paramName);

5. Then add the text node to the child element:

   param.addTextNode("To be, or not to be");

The SOAPMessage object has everything now and is ready to be sent to the service provider URL.

Last update: 2009.

Table of Contents

 About This Book

 Introduction to Web Service

 Introduction to SOAP (Simple Object Access Protocol)

 SOAP Message Structure

 SOAP Message Transmission and Processing

 SOAP Data Model

 SOAP Encoding

 SOAP RPC Presentation

 SOAP Properties Model

 SOAP Message Exchange Patterns

 SOAP HTTP Binding

 SOAP Perl Implementations

 SOAP PHP Implementations

 SOAP Java Implementations

 Perl SOAP::Lite - SOAP Server-Client Communication Module

 Perl Socket Test Program for HTTP and SOAP

 Perl SOAP::Lite for GetSpeech SOAP 1.1 Web Service

 Perl SOAP::Lite 0.710 for SOAP 1.2 Web Services

 Perl SOAP::Lite 0.710 for WSDL

 PHP SOAP Extension Client Programs

 PHP SOAP Extension Server Programs

 Java Socket and HttpURLConnection for SOAP

SAAJ - SOAP with Attachments API for Java

 SAAJ API 1.3 Classes and Interfaces Overview

 SAAJ API and Default Implementation in JDK 1.6.0

 SAAJ API Reference Implementation 1.3.4

 First SOAPConnection Test Program

 Creating SOAPConnection and SOAPMessage Objects

 SAAJ SOAPMessage Structure and Classes/Interfaces

Populating the SOAP Body with Request XML Elements

 Don't Use xml* as namespace Prefix

 addHeader() - Setting SOAPAction Header Line

 Calling GetSpeech SOAP 1.1 with SAAJ


 Calling GetSpeech SOAP 1.2 with SAAJ

 SoapUI - SOAP Web Service Testing Tool

 WS-Security - SOAP Message Security Extension

 WS-Security X.509 Certificate Token

 Web Services and SOAP Terminology


 PDF Printing Version