类型强制失败:无法将XMLList @ 106e9af1转换为mx.collections.IList

时间:2013-01-15 04:18:50

标签: web-services flex soap-client

使用代码自爆我试图解析以下的soap响应。

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:mx="library://ns.adobe.com/flex/mx"
               xmlns:components="components.*"
               xmlns:hellos="services.hellos.*"
               height="957"  creationComplete="initApp()" > 
    <fx:Style source="Styles.css"/>
    <fx:Script>

        <![CDATA[

            import mx.controls.Alert;

            private namespace invesbot = "http://Services.com";
            use namespace invesbot;

            private namespace a = "http://schemas.xmlsoap.org/soap/envelope/";
            private namespace b = "http://www.w3.org/2001/XMLSchema"; 
            private namespace c = "http://www.w3.org/2001/XMLSchema-instance";
            use namespace a;
            use namespace b;
            use namespace c;

            [Bindable]
            var _result:*



            private function initApp():void
            {
                myService.mycustomers();
            }

        ]]> 
    </fx:Script>
    <fx:Declarations>
        <mx:WebService id="myService" wsdl="http://localhost:8081/WebServiceTest/services/Hellos?wsdl" 
                       showBusyCursor="true" 
                       fault="Alert.show(event.fault.faultString), 'Error'">
            <mx:operation name="mycustomers" resultFormat="e4x">
                <mx:request>
                </mx:request>
            </mx:operation>
        </mx:WebService>

    </fx:Declarations>
<mx:HBox>
        <mx:Text
            text="{myService.mycustomers.lastResult.mycustomersReturn.name}"
            />
    </mx:HBox>
</s:Application>

SOAP响应如下

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope
  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soapenv:Body>
    <mycustomersResponse xmlns="http://Services.com">
      <mycustomersReturn>
        <age>28</age>
        <name>John</name>
      </mycustomersReturn>
      <mycustomersReturn>
        <age>29</age>
        <name>Alex</name>
      </mycustomersReturn>
      <mycustomersReturn>
        <age>30</age>
        <name>Jack</name>
      </mycustomersReturn>
    </mycustomersResponse>
  </soapenv:Body>
</soapenv:Envelope>

使用上面的代码,输出将是

<name xmlns="http://Services.com" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">John</name>
<name xmlns="http://Services.com" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Alex</name>
<name xmlns="http://Services.com" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Jack</name>

但是当我使用以下代码将结果放在下拉框中时,它会出现以下错误

<s:FormItem label="Employee:"> 
        <s:DropDownList id="dropDownList3"
                        labelField="name"
                        dataProvider ="{myService.mycustomers.lastResult.mycustomersReturn}"/>
    </s:FormItem>

TypeError:错误#1034:类型强制失败:无法将XMLList @ 106e9af1转换为mx.collections.IList。

1 个答案:

答案 0 :(得分:1)

您必须将数据包装在XMLListCollection中 与Arrays相同的是,你必须将它们包装到ArrayCollections中。 使用新版本的flex sdk 4.9,您还可以创建VectorLists和VectorCollections。

例如:

var iList:IList =  new XMLListCollection(myService.mycustomers.lastResult.mycustomersReturn);
dataProvider = iList;