SOAP 编码
SOAP 编码
SOAP包括用于编码数据类型的内置规则集.它使SOAP消息能够指示特定的数据类型,例如整数、浮点、双精度或阵列。
SOAP数据类型分为两大类——标量类型和复合类型。
标量类型包含一个值,如姓氏、价格或产品描述。
复合类型包含多个值,例如采购订单或股票报价列表。
复合类型进一步细分为数组和结构体。
SOAP消息的编码样式通过SOAP-ENV:encodingStyle attribute属性设置。
要使用SOAP 1.1编码,使用这个值 http://schemas.xmlsoap.org/soap/encoding/
要使用SOAP 1.2编码,使用这个值 http://www.w3.org/2001/12/soap-encoding
最新的SOAP规范采用了XML模式定义的所有内置类型。不过SOAP仍然保留自己的约定,用于定义不由XML架构标准化的构造,如数组和引用。
标量类型
对于标量类型,SOAP采用由XML模式规范指定的所有内置简单类型,包括字符串、浮点、双精度和整数。
下表列出了主要的简单类型,摘自XML架构:http://www.w3.org/TR/2000/WD-xmlschema-0-20000407/
类型 | 例子 |
---|---|
string | helloworld |
boolean | true, false, 1, 0. |
float | -INF, -1E4, -0, 0, 12.78E-2, 12, INF, NaN. |
double | -INF, -1E4, -0, 0, 12.78E-2, 12, INF, NaN. |
decimal | -1.23, 0, 123.4, 1000.00. |
binary | 100010 |
integer | -126789, -1, 0, 1, 126789. |
nonPositiveInteger | -126789, -1, 0. |
negativeInteger | -126789, -1. |
long | -1, 12678967543233 |
int | -1, 126789675 |
short | -1, 12678 |
byte | -1, 126 |
nonNegativeInteger | 0, 1, 126789 |
unsignedLong | 0, 12678967543233 |
unsignedInt | 0, 1267896754 |
unsignedShort | 0, 12678 |
unsignedByte | 0, 126 |
positiveInteger | 1, 126789. |
date | 1999-05-31, ---05. |
time | 13:20:00.000, 13:20:00.000-05:00 |
例如,这里是一个带有双数据类型的SOAP响应,
<?xml version='1.0' encoding='UTF-8'?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2001/12/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <SOAP-ENV:Body> <ns1:getPriceResponse xmlns:ns1="urn:examples:priceservice" SOAP-ENV:encodingStyle="http://www.w3.org/2001/12/soap-encoding"> <return xsi:type="xsd:double">54.99</return> </ns1:getPriceResponse> </SOAP-ENV:Body> </SOAP-ENV:Envelope>
复合类型
SOAP数组具有非常特定的规则集,它要求您指定元素类型和数组大小。soap还支持多维数组,但不是所有soap实现都支持多维功能。
要创建一个数组,必须将其指定为xsi:数组类型。该数组还必须包括arraytype属性。需要此属性为包含元素和数组的维度指定数据类型。
例如,以下属性指定了10个双值的数组:
arrayType = "xsd:double[10]"
相反,以下属性指定字符串的二维数组:
arrayType = "xsd:string[5,5]"
下面是一个具有双重值数组的soap响应示例:
<?xml version='1.0' encoding='UTF-8'?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2001/12/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <SOAP-ENV:Body> <ns1:getPriceListResponse xmlns:ns1="urn:examples:pricelistservice" SOAP-ENV:encodingStyle="http://www.w3.org/2001/12/soap-encoding"> <return xmlns:ns2="http://www.w3.org/2001/09/soap-encoding" xsi:type="ns2:Array" ns2:arrayType="xsd:double[2]"> <item xsi:type="xsd:double">54.99</item> <item xsi:type="xsd:double">19.99</item> </return> </ns1:getPriceListResponse> </SOAP-ENV:Body> </SOAP-ENV:Envelope>
结构体包含多个值,但每个元素都使用唯一的访问器元素指定的。例如,在产品目录中考虑一个项目,在这种情况下,结构可能包含产品SKU、产品名称、描述和价格。下面是这样一个结构在SOAP消息中的表示方式:
<?xml version='1.0' encoding='UTF-8'?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2001/12/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <SOAP-ENV:Body> <ns1:getProductResponse xmlns:ns1="urn:examples:productservice" SOAP-ENV:encodingStyle="http://www.w3.org/2001/12/soap-encoding"> <return xmlns:ns2="urn:examples" xsi:type="ns2:product"> <name xsi:type="xsd:string">Red Hat Linux</name> <price xsi:type="xsd:double">54.99</price> <description xsi:type="xsd:string"> Red Hat Linux Operating System </description> <SKU xsi:type="xsd:string">A358185</SKU> </return> </ns1:getProductResponse> </SOAP-ENV:Body> </SOAP-ENV:Envelope>
注意:请注意在编写您的SOAP代码时适当地缩进。结构中的每个元素都用唯一的访问器名称指定。例如,上面的消息包括四个访问者元素—名称、价格、描述和SKU。每个元素都可以有自己的数据类型。例如,名称指定为字符串,而价格指定为double。