14 Replies Latest reply on Aug 20, 2007 7:24 AM by ebelair

    Getting my data from XML file into bindable ArrayCollection?

    John Cambridge
      I have the following ArrayCollection set up:
      public var expenses:ArrayCollection = new ArrayCollection([
      {Hour: "12 AM", Average: 6, Today: 5},
      {Hour: "1 AM", Average: 4, Today: 1},
      {Hour: "2 AM", Average: 3, Today: 2},
      {Hour: "3 AM", Average: 1, Today: 2},
      {Hour: "4 AM", Average: 1, Today: 1}, ......

      All of my other code works fine to display the data in this ArrayCollection. I need to update this ArrayCollection based on an XML file, however all of the code I've tried keeps giving me warnings such as "Data binding will not be able to detect assignments." Can someone show me what general code I could use to get data from an XML file into an ArrayCollection of that format, and what the XML file would look like?

      Thanks!
        • 1. Re: Getting my data from XML file into bindable ArrayCollection?
          ntsiii Level 3
          Why do you need to put the data into an ArrayCollection? Why not leave it XML?

          If you really MUST put it in an ArrayCollection, then use the ArrayCollection API.

          It does not matter what the xml format is. Loop over the XMLList and assign the text node or attribute values to the ArrayCollection element properties.

          Tracy
          • 2. Re: Getting my data from XML file into bindable ArrayCollection?
            John Cambridge Level 1
            Thanks for your reply. Do you know where I can find an example of where to do this properly? From all my attempts at doing this, I generally get the warning "Data binding will not be able to detect assignments."
            • 3. Re: Getting my data from XML file into bindable ArrayCollection?
              Hello,

              the mesage
              quote:

              "Data binding will not be able to detect assignments."
              typically appears, if you "forget" the "[Bindable] before the declaration of an array (e.g.).
              Is your ArrayCollection "bindable"?
              • 4. Re: Getting my data from XML file into bindable ArrayCollection?
                John Cambridge Level 1
                No, I didn't forget the [Bindable] part.
                • 5. Re: Getting my data from XML file into bindable ArrayCollection?
                  ntsiii Level 3
                  "... example of where to do this properly..." exactly what do you mean by "this"? Looping? Using the ArrayCollection API?

                  "...warning "Data binding will not be able to detect assignments..." That is not related to populating an ArrayCollection. Post the code that is causing it.

                  And I still suspect you may be making a mistake.

                  Tracy
                  • 6. Re: Getting my data from XML file into bindable ArrayCollection?
                    John Cambridge Level 1
                    Thanks for your reply. Actually I got it to work based on all of your suggestions. Thanks!
                    • 7. Re: Getting my data from XML file into bindable ArrayCollection?
                      ebelair Level 1
                      I'm receiving the same error when trying to use a node of an Object/Array as an argument being sent to a WebService.

                      My Code:
                      _________________________________________________________

                      <?xml version="1.0" encoding="utf-8"?>
                      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" styleName="plain" xmlns:inc="*" creationComplete="init();">

                      <mx:Script>
                      <![CDATA[

                      [Bindable] private var wsInput:Array;

                      private function handleEvent(eventObj:Event):void
                      {
                      // Create an object to dynamically set arguments to be passed to the WebService
                      wsInput = new Array();
                      wsInput.print = "yes";

                      ]]>
                      </mx:Script>

                      <mx:WebService id="qPositions" wsdl=" http://localhost/private/eric_functions.cfc?wsdl" showBusyCursor="true">
                      <mx:operation name="my_function" result="handleReportResult(event)" fault="Alert.show(event.fault.message)" >
                      <mx:request xmlns="">
                      <print>{wsInput.print}</print>
                      </mx:request>
                      </mx:operation>
                      </mx:WebService>

                      _________________________________________________________

                      I also tried outputting the value in a TextArea, and nothing was displayed:

                      <mx:TextArea id="txtArea" x="20" y="550" width="95%" height="200" text="{wsInput.print}"/>

                      _________________________________________________________

                      When I use a Bindable variable as the text value for the TextArea, and set that variable to wsInput.print, it displays fine.

                      I can also get the entire Object to display (ObjectUtil.toString(wsInput)).

                      What am I doing wrong???
                      • 8. Re: Getting my data from XML file into bindable ArrayCollection?
                        ntsiii Level 3
                        Some observations:
                        "wsInput.print" is typed as an array, but you are attempting to access a property, "print" of it.

                        You cannot bind to properties of an Object class instance. this is probably what is causing the warning.

                        Avoid this by programaticlly creating the request object, instead of doing it declaratively.

                        Tracy
                        • 9. Re: Getting my data from XML file into bindable ArrayCollection?
                          ebelair Level 1
                          Thank you for the reply.

                          How would I create the request object programmatically? By using ActionScript? Could you show me a quick code snippet of how to do this? Or point me in the right direction?

                          Thanks agian.
                          • 10. Re: Getting my data from XML file into bindable ArrayCollection?
                            ntsiii Level 3
                            Here ya go. Specifically, look in the "Invoke send" section. This is for HTPService but works the same for WebService.

                            Tracy

                            Sample code using HTTPService, e4x, handler function to populate a list item.
                            Also shows usage of AsyncToken.

                            The DataGrid tag:
                            <mx:DataGrid id="dg" dataProvider="{_xlcMyListData}" .../>


                            The HTTPService tag:
                            <mx:HTTPService id="service" resultFormat="e4x" result="onResult(event)" fault="..../>

                            Script block declaration:
                            import mx.rpc.Events.ResultEvent;
                            [Bindable]private var _xlcMyListData:XMLListCollection;

                            Invoke send:
                            var oRequest:Object = new Object();
                            oRequest.Arg1 = "value1";
                            var callToken:AsyncToken = service.send(oRequest);
                            token.callId = "myQuery1";

                            Result Handler function:
                            private function onResult(oEvent:ResultEvent):void {
                            var xmlResult:XML = XML(event.result); //converts result Object to XML. can also use "as" operator
                            var xlMyListData:XMLList = xmlResult.myListData; //depends on xml format, is row data
                            _xlcMyListData = new XMLListCollection(xlMyListData); //wrap the XMLList in a collection
                            trace(_xlcMyListData.toXMLString()); //so you can see exactly how to specify dataField or build labelFunction
                            var callToken:AsyncToken = oEvent.token;
                            var sCallId = callToken.callId; //"myQuery1"
                            switch(sCallId) {
                            case "myQuery1":
                            doQuery2();
                            break;
                            ...
                            }
                            }//onResult
                            • 11. Getting my data from XML file into bindable ArrayCollection?
                              ebelair Level 1
                              I'm confused by several pieces of your code. First, I'm using a WebService - how do you specify which function you are calling? Second, what does the "doQuery2()" function do?? I already have a result handler, can I just ignore this?

                              Also, I am trying to set a object property to an empty string, like so:

                              wsInput.arg2 = "";

                              I get an error during compilation stating that a label must be a simple identifier.
                              • 12. Re: Getting my data from XML file into bindable ArrayCollection?
                                ntsiii Level 3
                                Declare your operation in mxml (or AS if you prefer). Then the send call is:
                                myWS.myOperation.send(oREquest)

                                "what does the "doQuery2()" just an example of what can be done. Ignore it.

                                What is wsInput? It must be an Object, but the name prefix, "ws" implies it is a webservice. Post a bit more code.

                                Tracy

                                • 13. Getting my data from XML file into bindable ArrayCollection?
                                  ebelair Level 1
                                  Ok, I've got the code with the Object compiling correctly now, however, when I run the app, I get the following error returned from the WebService:

                                  "faultCode:EncodingError faultString:'Array of input arguments did not contain a required parameter at position 1' faultDetail:'null'"

                                  My code:

                                  ______________________________________________________

                                  <?xml version="1.0" encoding="utf-8"?>
                                  <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" styleName="plain" xmlns:inc="*" creationComplete="init();">

                                  <mx:Script>
                                  <![CDATA[

                                  private function handleEvent(eventObj:Event):void
                                  {
                                  // Create an object to dynamically set arguments to be passed to the WebService
                                  var wsInput:Object = new Object();
                                  wsInput.print = "yes";
                                  wsInput.format = "";

                                  qPositions.my_function.send(wsInput);

                                  ]]>
                                  </mx:Script>

                                  <mx:WebService id="qPositions" wsdl=" http://localhost/private/eric_functions.cfc?wsdl" showBusyCursor="true">
                                  <mx:operation name="my_function" result="handleReportResult(event)" fault="Alert.show(event.fault.message)" >
                                  <mx:request xmlns=""/>
                                  </mx:operation>
                                  </mx:WebService>

                                  ______________________________________________________
                                  • 14. Re: Getting my data from XML file into bindable ArrayCollection?
                                    ebelair Level 1
                                    Nevermind, I got it to work!

                                    Instead of this:
                                    ____________________________________

                                    qPositions.my_function.send(wsInput);
                                    ____________________________________

                                    I did this:
                                    ____________________________________

                                    var op:mx.rpc.AbstractOperation = qPositions["sp_s_position_xml"];

                                    op.arguments = wsInput;
                                    op.send();
                                    ____________________________________

                                    Thanks for all your help! Now onto my Advanced DataGrid display/rendering issue....