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 http://www.w3.org/TR/REC-xml-names/
An explicit XML namespace declaration takes the following form:
<prefix:myElement 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="http://schemas.xmlsoap.org/soap/envelope/"
This declaration defines SOAP_ENV as an alias for the namespace:
http://schemas.xmlsoap.org/soap/envelope/
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 http://schemas.sxmlsoap.org/soap/envelope/URI .
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle= "http://schemas.xmlsoap.org/soap/encoding/"> <SOAP-ENV:Header> <HeaderA xmlns="HeaderURI" SOAP-ENV:mustUnderstand="0"> The text of the header </HeaderA> </SOAP-ENV:Header> <SOAP-ENV:Body> . . . </SOAP-ENV:Body> </SOAP-ENV:Envelope>
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.
SOAP defines two namespaces:
The SOAP envelope, the root element of a SOAP message, has the following namespace identifier:
"http://schemas.xmlsoap.org/soap/envelope"
The SOAP serialization, the URI defining SOAP’s serialization rules, has the following namespace identifier:
"http://schemas.xmlsoap.org/soap/encoding"
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.
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", "GetLTP","http://foo.eztrade.com");
This would be equivalent to the namespace declaration:
<GetLTP:TradePrice xmlns:GetLTP= "http://foo.eztrade.com">
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", "http://foo.eztrade.com"); SOAPBodyElement gltp = body.addBodyElement(bodyName);
For any given Name object, you can use the following Name methods to parse the name:
getQualifiedName returns "prefix:LocalName ", for the given name, this would be GetLTP:TradePrice.
getURI would return "http://foo.eztrade.com" .
getLocalName would return "TradePrice ".
getPrefix would return "GetLTP".