Don't Use xml* as namespace Prefix

This section provides a tutorial example on how to create the body element and sub elements with your own namespaces and namespace prefixes. SAAJ does not support xml* as namespace prefix for your body element.

While trying to populate the SOAP body with the "GetSpeech" request elements, I noticed the following very interesting issue with SAAJ 1.3.4.

If you use "xml" as initial characters for the namespace prefix for your SOAPBodyElement object, the namespace declaration will not be included in the final SOAP XML message.

To demonstrate this issue, I wrote this testing program:

/**
 * SOAPBodyElementNamespace.java
 * Copyright (c) 2009 by Dr. Herong Yang, herongyang.com
 * All rights reserved
 */
import java.io.*;
import javax.xml.soap.*;
public class SOAPBodyElementNamespace {
   public static void main(String[] args) {
      PrintStream out = System.out;
      try {
         String prefix = "xmlme";
         String uri = "http://xmlme.com/WebServices";
         SOAPMessage reqMsg = generateMsg(prefix, uri);
         out.println("\n\n");
         out.println("Test 1: "+prefix+"="+uri);
         reqMsg.writeTo(out);

         prefix = "tns";
         uri = "http://xmlme.com/WebServices";
         reqMsg = generateMsg(prefix, uri);
         out.println("\n\n");
         out.println("Test 2: "+prefix+"="+uri);
         reqMsg.writeTo(out);

         prefix = "xmlAnything";
         uri = "http://xmlme.com/WebServices";
         reqMsg = generateMsg(prefix, uri);
         out.println("\n\n");
         out.println("Test 3: "+prefix+"="+uri);
         reqMsg.writeTo(out);
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
   public static SOAPMessage generateMsg(String prefix, String uri)
      throws Exception {
      SOAPMessage reqMsg
         = MessageFactory.newInstance().createMessage();
      SOAPEnvelope envelope = reqMsg.getSOAPPart().getEnvelope();
      SOAPBody body = envelope.getBody();
      SOAPBodyElement service = body.addBodyElement(
         envelope.createName("GetSpeech", prefix, uri));
      SOAPElement param = service.addChildElement(
         envelope.createName("Request", prefix, uri));
      param.addTextNode("To be, or not to be");
      return reqMsg;
   }
}

Here is the execution result:

Test 1: xmlme=http://xmlme.com/WebServices
<SOAP-ENV:Envelope
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  <SOAP-ENV:Header/>
  <SOAP-ENV:Body>
    <xmlme:GetSpeech>
      <xmlme:Request>To be, or not tobe</xmlme:Request>
    </xmlme:GetSpeech>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Test 2: tns=http://xmlme.com/WebServices
<SOAP-ENV:Envelope
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  <SOAP-ENV:Header/>
  <SOAP-ENV:Body>
    <tns:GetSpeech xmlns:tns="http://xmlme.com/WebServices">
      <tns:Request>To be, or not to be</tns:Request>
    </tns:GetSpeech>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Test 3: xmlAnything=http://xmlme.com/WebServices
<SOAP-ENV:Envelope
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  <SOAP-ENV:Header/>
  <SOAP-ENV:Body>
    <xmlAnything:GetSpeech>
      <xmlAnything:Request>To be, or not to be</xmlAnything:Request>
    </xmlAnything:GetSpeech>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

My conclusions are:

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

 SOAPConstants.SOAP_1_2_PROTOCOL

 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

 References

 PDF Printing Version