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:

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

         prefix = "tns";
         uri = "";
         reqMsg = generateMsg(prefix, uri);
         out.println("Test 2: "+prefix+"="+uri);

         prefix = "xmlAnything";
         uri = "";
         reqMsg = generateMsg(prefix, uri);
         out.println("Test 3: "+prefix+"="+uri);
      } catch (Exception e) {
   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=
      <xmlme:Request>To be, or not tobe</xmlme:Request>

Test 2: tns=
    <tns:GetSpeech xmlns:tns="">
      <tns:Request>To be, or not to be</tns:Request>

Test 3: xmlAnything=
      <xmlAnything:Request>To be, or not to be</xmlAnything:Request>

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


 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