- SOAP 教程
- SOAP - 首頁
- SOAP - 什麼是 SOAP?
- SOAP - 訊息
- SOAP - 信封
- SOAP - 報頭
- SOAP - 正文
- SOAP - 故障
- SOAP - 編碼
- SOAP - 傳輸
- SOAP - 示例
- SOAP - 標準
- SOAP 資源
- SOAP - 快速指南
- SOAP - 有用資源
- SOAP - 討論
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 Schema 未標準化的結構,例如陣列和引用。
標量型別
對於標量型別,SOAP 採用 XML Schema 規範指定的所有內建簡單型別。這包括字串、浮點數、雙精度數和整數。
下表列出了主要簡單型別,摘自 XML Schema 第 0 部分 - Primer http://www.w3.org/TR/2000/WD-xmlschema-0-20000407/
| XML Schema 內建的簡單型別 | ||
|---|---|---|
| 簡單型別 | 示例 | |
| string | 確認這是電動的。 | |
| 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: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。每個元素可以具有自己的資料型別。例如,名稱指定為字串,而價格指定為雙精度數。