解析XML和XMLNS

在不知道夫节点和属性情况下,查找XML中某节点或属性

 1 var xml:XML = <order>
 2                 <item name="00">
 3                     <item1 >
 4                         <item2 name="22">222</item2>
 5                     </item1>
 6                 </item>
 7                    <item>
 8                     <item1>123</item1>
 9                     <item2>222</item2>
10                 </item>
11                 </order>;
12 //查找节点
13 trace(xml..item)
14 trace(xml.descendants("item"))
15 //查找属性
16 trace(xml..@["name"])
17 for each (var i:String in xml.descendants("item1"))
18 {
19     trace(i);
20 }

读取WebService返回的数据位XMLNS,即XML Namespaces的缩写,具有有命名空间的XML。

至于如何连接WebService和读取数据,可以用这封装好的WebService类包:http://labs.alducente.com/gophr/ 。

xmlns解析代码如下:

 1 import alducente.services.WebService;
 2 import flash.events.*;
 3 
 4 var ws:WebService = new WebService();
 5 ws.addEventListener(Event.CONNECT, connected);
 6 ws.connect("http://searcher.ce4.cn/mobile/default.asmx?wsdl");
 7 ws.cacheResults = true;
 8 
 9 var initTime:Number;
10 var serviceRespone:XML;
11 
12 function connected(evt:Event):void
13 {
14     initTime = getTimer();
15     ws.GetTotals(getTotals,"12");
16     ws.GetDataList(getDataList,"广州",1,10);
17 }
18 //获取信息总数
19 function getTotals(serviceRespone:XML):void
20 {
21     //trace(serviceRespone);
22     var soapNS:Namespace = serviceRespone.namespace("soap");
23     var wNS:Namespace = new Namespace("w","http://tempuri.org/");
24     serviceRespone.addNamespace(wNS);
25     var totals:XMLList = serviceRespone.soapNS::Body.wNS::GetTotalsResponse.wNS::GetTotalsResult;
26     //trace(totals)
27 }
28 //获取详细信息
29 function getDataList(serviceRespone:XML):void
30 {
31     trace(serviceRespone);
32     var soapNS:Namespace = serviceRespone.namespace("soap");
33     var wNS:Namespace = new Namespace("w","http://tempuri.org/");
34     var title:XMLList = new XMLList();
35     var content:XMLList = new XMLList();
36     var prevurl:XMLList = new XMLList();
37     serviceRespone.addNamespace(wNS);
38     //命名空间::节点名
39     title = serviceRespone.soapNS::Body.wNS::GetDataListResponse.wNS::GetDataListResult.wNS::DataInfo.wNS::title;
40     content = serviceRespone.soapNS::Body.wNS::GetDataListResponse.wNS::GetDataListResult.wNS::DataInfo.wNS::content;
41     prevurl = serviceRespone.soapNS::Body.wNS::GetDataListResponse.wNS::GetDataListResult.wNS::DataInfo.wNS::prevurl;
42     for each (var item_content in content)
43     {
44         trace(item_content);
45     }
46 }

 

 

posted @ 2012-10-30 18:01  FlashHK  阅读(605)  评论(0编辑  收藏  举报