SOAP - 编码
SOAP 包含一组内置的数据类型编码规则。它使 SOAP 消息能够指示特定的数据类型,例如整数、浮点数、双精度数或数组。
- SOAP 数据类型分为两大类:标量类型和复合类型。 
- 标量类型仅包含一个值,例如姓氏、价格或产品描述。 
- 复合类型包含多个值,例如采购订单或股票报价列表。 
- 复合类型进一步细分为数组和结构体。 
- SOAP 消息的编码样式是通过SOAP-ENV:encodingStyle属性设置的。 
- 要使用 SOAP 1.1 编码,请使用值http://schemas.xmlsoap.org/soap/encoding/ 
- 要使用 SOAP 1.2 编码,请使用值http://www.w3.org/2001/12/soap-encoding 
- 最新的SOAP规范采用了XML Schema定义的所有内置类型。尽管如此,SOAP 仍然保留了自己的约定来定义 XML 模式未标准化的构造,例如数组和引用。 
标量类型
对于标量类型,SOAP 采用 XML Schema 规范指定的所有内置简单类型。这包括字符串、浮点数、双精度数和整数。
下表列出了主要的简单类型,摘自 XML 架构第 0 部分 - 入门http://www.w3.org/TR/2000/WD-xmlschema-0-20000407/
| XML 架构中内置的简单类型 | ||
|---|---|---|
| 简单型 | 例子) | |
| 细绳 | 确认这是电动的。 | |
| 布尔值 | 真、假、1、0。 | |
| 漂浮 | -INF、-1E4、-0、0、12.78E-2、12、INF、NaN。 | |
| 双倍的 | -INF、-1E4、-0、0、12.78E-2、12、INF、NaN。 | |
| 小数 | -1.23、0、123.4、1000.00。 | |
| 二进制 | 100010 | |
| 整数 | -126789, -1, 0, 1, 126789。 | |
| 非正整数 | -126789, -1, 0。 | |
| 负整数 | -126789,-1。 | |
| 长的 | -1, 12678967543233 | |
| 整数 | -1, 126789675 | |
| 短的 | -1, 12678 | |
| 字节 | -1, 126 | |
| 非负整数 | 0, 1, 126789 | |
| 无符号长整型 | 0, 12678967543233 | |
| 无符号整数 | 0, 1267896754 | |
| 无符号短整型 | 0, 12678 | |
| 无符号字节 | 0, 126 | |
| 正整数 | 1、126789。 | |
| 日期 | 1999-05-31, ---05. | |
| 时间 | 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:type数组。该数组还必须包含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。每个元素可以有自己的数据类型。例如,名称指定为字符串,而价格指定为双精度。
