教程 > SOAP > SOAP 基本教程 阅读:80

SOAP 编码 Encoding

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 Schema 标准化的构造,例如数组和引用。

Scalar 类型

对于标量类型,SOAP 采用 XML Schema 规范指定的所有内置简单类型。 这包括字符串、浮点数、双精度数和整数。

下表列出了主要的简单类型,摘自 XML Schema Part 0 - Primer http://www.w3.org/TR/2000/WD-xmlschema-0-20000407/

简单的类型 示例
string Confirm this is electric.
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>

Compound 类型

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。 每个元素都可以有自己的数据类型。 例如,name 指定为字符串,而 price 指定为 double。

查看笔记

扫码一下
查看教程更方便