19.7.如何处理XML服务里所返回的命名空间

19.7.1. 问题
如何从一个自定义的命名空间及扩展的Web 服务里, 分析其返回的XML?
19.7.2. 解决办法
申明一个命名空间变量, 将它设置为返回的XML 命名空间的位置, 然后在进行任XML 处理之前, 先调用该命名空间的”use”方法。
19.7.3. 讨论
对包含自定义命名空间的XML 进行分析是比较困难的, 它要求该命名空间一定要在任何XML 返回前被申明, 同时要在该命名空间内进行分析。不少Web 服务都会返回一些包含命名空间的声名, 好像以下的例子:
HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: nnn
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
<soap:Body xmlns:m="http://www.example.org/stock">
<m:PriceResult>
<m:Price>34.5</m:Price>
</m:PriceResult>
</soap:Body>
</soap:Envelope>
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"

以上所申明的代码行一定要申明在XML 被处理之前。这是通过如下的命名空间声明来达成的:
private namespace w3c = "http://www.w3.org/2001/12/soap-envelope";
use namespace w3c;
To access the Price node of the above SOAP response, use the namespace title in the E4X statement as shown here:

+展开
-ActionScript
var prices:XMLList = xml.m::PriceResult.m::Price;
通过”.”运算符可以存取任何使用限定命名空间的XML 的子节点, 同时该命名空间申明时应跟随着”::”运算符及节点名义。例如以下的XML 对象:
+展开
-XML
<m:PriceResult>
<m:Price>34.5</m:Price>
</m:PriceResult>

“price”节点会通过以下的方法来存取:
+展开
-ActionScript
m::PriceResult.m::Price

加支付宝好友偷能量挖...


评论(0)网络
阅读(107)喜欢(0)flash/flex/fcs/AIR