Sun Java System Message Queue 4.1 Developer's Guide for Java Clients


An XML namespace is a means of qualifying element and attribute names to disambiguate them from other names in the same document. This section provides a brief description of XML namespaces and how they are used in SOAP. For complete information, see

An explicit XML namespace declaration takes the following form:

xmlns:prefix ="URI">

The declaration defines prefix as an alias for the specified URI. In the element myElement, you can use prefix with any element or attribute to specify that the element or attribute name belongs to the namespace specified by the URI.

The following is an example of a namespace declaration:

<SOAP-ENV:Envelope xmlns:SOAP-ENV=""

This declaration defines SOAP_ENV as an alias for the namespace:

After defining the alias, you can use it as a prefix to any attribute or element in the Envelope element. In Example 5–1, the elements <Envelope> and <Body> and the attribute encodingStyle all belong to the SOAP namespace specified by the .

Example 5–1 Explicit Namespace Declarations

      The text of the header

Note that the URI that defines the namespace does not have to point to an actual location; its purpose is to disambiguate attribute and element names.

Pre-defined SOAP Namespaces

SOAP defines two namespaces:

When you use SAAJ to construct or consume messages, you are responsible for setting or processing namespaces correctly and for discarding messages that have incorrect namespaces.

Using Namespaces when Creating a SOAP Name

When you create the body elements or header elements of a SOAP message, you must use the Name object to specify a well-formed name for the element. You obtain a Name object by calling the method SOAPEnvelope.createName.

When you call this method, you can pass a local name as a parameter or you can specify a local name, prefix, and URI. For example, the following line of code defines a name object bodyName.

Name bodyName = MyEnvelope.createName("TradePrice",

This would be equivalent to the namespace declaration:

<GetLTP:TradePrice xmlns:GetLTP= "">

The following code shows how you create a name and associate it with a SOAPBody element. Note the use and placement of the createName method.

SoapBody body = envelope.getBody();//get body from envelope
Name bodyName = envelope.createName("TradePrice", "GetLTP",
SOAPBodyElement gltp = body.addBodyElement(bodyName);

Parsing Name Objects

For any given Name object, you can use the following Name methods to parse the name: