3 Replies Latest reply on Jul 14, 2006 6:44 AM by mike2281

    Extracting Array of objects from web service

    mike2281 Level 1
      After a couple days of head banging I now have a webservice call executing and I am trying to extract / create a class from the ResultEvent:

      If the xml returned from the web service is:
      <person name="Mike" />
      <person name="Dave" />

      and the class is:
      class Person
      public var Name:String;

      The result event is:
      private function doResults(result:ResultEvent):void
      // how do I create an array of Person objects from result, I would also like to know how to create a typed array something like class PersonList if possible. I just need the raw how to loop the result and set the values on the class


        • 1. Re: Extracting Array of objects from web service
          inlineblue Level 1
          There's no templating in AS3, so you can't create a typed array. If you want to make an array of Person objects, then you just gotta loop through and do it manually.

          If your webservice returned the XML as a string, then you'll need to create an XML object:

          var xml: XML = new XML(result.result); //Skip this if already an XML object
          var xmlPersons: XMLList = xml.person; //Grabs all <person> nodes
          var persons: Array = new Array(); //The array we're creating
          for (var n: int = 0; n < xmlPersons.length(); ++n)
          var per: Person = new Person();
          per.Name = xmlPersons[n].@name; //Get the name attribute
          person.push(per); //Add to array

          Completely untested, but that's the general idea.

          Also, if you use RemoteObject with ColdFusion/FDS, there's a mechanism where you can map server objects to client-side objects. I haven't used it, so you'll have to check the docs for the RemoteClass tag.
          • 2. Re: Extracting Array of objects from web service
            ntsiii Level 3
            import myClasses.Person;
            var xmlResult:XML = new XML(result.toString())
            var personCur:Person
            var aPersons:Array = new Array();
            var nodePerson:XML
            for (nodePerson in xmlResult.person) {
            personCur = new Person()
            personCur.Name = nodePerson.@name;
            • 3. Re: Extracting Array of objects from web service
              mike2281 Level 1
              Well I wound up with just trial and error until I got it working, Im sure this will be improved as I go, but it's enough to press on with the app for now, this code is in the result function and result is the ResultEvent wich appears to be an array of generic objects representing the objects returned by the service. This in no way uses FDS and is talking to a simple dotnet / asp.net web service.

              // here app is a singleton class for app level data it is bindable and is used to update the view which in this example is bound to a public var MyObjects which in this case is an ArrayCollection, I unbox to MyObject in a custom view control
              for (var s:String in result.result)
              m = new MyObject();
              m.ID = result.result[s].ID;
              m.Name = result.result[s].Name;
              m.Type = result.result[s].Type;

              It also appears that you should create the WebService one time, and store refrences to the methods during app startup, the methods are called Operations and it appears you set the result and fault events then call send on the operation to get the data.

              // store this in a singleton app class
              var ws:WebService = new WebService();
              ws.wsdl = " http://dev.domain.com/WebService.asmx?WSDL";

              // this is the operation which is also stored in the singleton app class
              var op:AbstractOperation = ws.getOperation("GetModules");

              // elsewere in the app we get the operation refrence setup result and fault and call send, then copy the generic object into our cutom classes using the first chunk of code above

              I thought I would post this as I could find no such example in the offline or online docs, If anyone has a better way or see's a problem please post.

              I hope helps others with non FDS service calls and I look forward to hearing comments.