4 Replies Latest reply on Oct 4, 2008 8:23 PM by PaulBHartzog

    amfphp and flex - parsing a struct

    PaulBHartzog
      I am using an amfphp service, which I call from Flex. The service is embedded in a drupal site, and it calls a function named getTree() which returns a php "struct"

      I can pull out vars from the struct and show them in a DataGrid (in columns), but since I don't know how deep the struct's hierarchy is, what I really need is a way to iterate through it and load all the items into something I can deal with (an ArrayCollection???)

      1. what should I use to store the data (ArrayCollection, Array, etc.) and why?
      2. what code will repeatedly iterate through a tree hierarchy until it finds all items?

      much thx,
      -p
        • 1. Re: amfphp and flex - parsing a struct
          Level 7

          "PaulBHartzog" <webforumsuser@macromedia.com> wrote in message
          news:gc820i$doj$1@forums.macromedia.com...
          >I am using an amfphp service, which I call from Flex. The service is
          >embedded
          > in a drupal site, and it calls a function named getTree() which returns a
          > php
          > "struct"
          >
          > I can pull out vars from the struct and show them in a DataGrid (in
          > columns),
          > but since I don't know how deep the struct's hierarchy is, what I really
          > need
          > is a way to iterate through it and load all the items into something I can
          > deal
          > with (an ArrayCollection???)
          >
          > 1. what should I use to store the data (ArrayCollection, Array, etc.) and
          > why?
          > 2. what code will repeatedly iterate through a tree hierarchy until it
          > finds
          > all items?

          (1) I'd go with HierarchicalData for 1000, Alex.

          (2) What is "recursion"?

          ;-)


          • 2. Re: amfphp and flex - parsing a struct
            Peter Lorent Level 2
            That would depend an the struct that is returned. I'm getting arrays returned containing objects, see attached dump from trace(ObjectUtil.toString(event.result)); in a result handler on the RemoteObject.

            I'm parsing the result into an Array and somewhere else in the program I use this array to create an ArrayCollection to serve as a dataprovider for a list.
            • 3. Re: amfphp and flex - parsing a struct
              Level 7

              "LuigiL" <webforumsuser@macromedia.com> wrote in message
              news:gc8748$jrm$1@forums.macromedia.com...
              > That would depend an the struct that is returned. I'm getting arrays
              > returned
              > containing objects, see attached dump from
              > trace(ObjectUtil.toString(event.result)); in a result handler on the
              > RemoteObject.
              >
              > I'm parsing the result into an Array and somewhere else in the program I
              > use
              > this array to create an ArrayCollection to serve as a dataprovider for a
              > list.
              >
              >
              > private function onCategoriesResult(event:ResultEvent):void
              > {
              > trace(ObjectUtil.toString(event.result));
              >
              > if(event.result.length > 0) {
              > for(var i:uint = 0; i < event.result.length; i++) {
              > var category:Category = new Category(event.result .product_category_id,
              > event.result
              .name);
              > _categories.push(category);
              > }
              > broadcastCategoriesLoaded();
              > }
              > }
              >
              > private function broadcastCategoriesLoaded():void
              > {
              > dispatchEvent(new Event(RemotingCall.CATEGORY_DATA_LOADED));
              > }

              That's not really recursive. I didn't have time to go into my code and look
              anything up earlier this morning, but here's some code that recurses XML and
              parses it into custom VO's, MenuContainers. Each MenuContainer has a
              children collection that can contain Task VO's (technically TaskUID's which
              are Task VO's decorated with IUID) or other MenuContainers:

              private function parseTaskXML(taskXML:XML):void{
              //recursively loads a menu container with task VO's
              //this also loads the singleton TaskManager for use
              //across the app
              _allTasks=getChildrenFromXML(taskXML.children());
              }
              private function getChildrenFromXML(childXML:XMLList):ArrayCollection{
              var childObj:*;
              var dateStr:String;
              var theDate:Date;
              var splitDateStr:Array;
              var tmpID:int;
              var tmpDesc:String;
              var tmpRunMode:String
              var resultAC:ArrayCollection = new ArrayCollection();

              for each (var child:XML in childXML){
              switch (child.name().toString()){
              case 'task':
              //parse date info into a true date
              if (child.child('taskDate').length()>0){
              //temporary string with the date in it in the
              //format mm/dd/yyyy
              dateStr=child.child('taskDate')[0].text();
              splitDateStr=dateStr.split('/');
              theDate = new Date(splitDateStr[2], int(splitDateStr[0]) - 1,
              splitDateStr[1]);
              } else {
              theDate = null;
              }
              /* If the task has its own runmode, use that.
              Otherwise, use the container run mode */
              if (child.child('runMode').length()>0){
              tmpRunMode=child.child('runMode')[0].text();
              } else {
              tmpRunMode = child.parent().attribute('runMode');
              }
              //Alert.show(child.toXMLString(), int(child.levelID[0]).toString());
              childObj = new Task(int(child.@id), child.idString,
              int(child.levelID[0]),
              child.desc,
              Task.isCompleteStringToConst(child.isComplete),
              child.isActive == 'true' ? true : false, theDate,
              child.isBookmarked == 'true' ? true : false,
              tmpRunMode);
              taskManager.tasks.addItem(childObj);
              //decorate with IUID to allow easier indexing in HierarchicalData
              childObj = new TaskUID(childObj);
              break;
              case 'container':
              childObj = new MenuContainer(int(child.@id), child.@desc,
              child.@runMode);
              //continue recursing until all container children have been accounted
              for
              if (child.children().length()>0){
              childObj.children=getChildrenFromXML(child.children());
              }
              break;
              default:
              //default object
              tmpID=0;
              if (child.attribute('id').length()>0){
              tmpID = int(child.@id)
              } else if (child.child('id')){
              tmpID = int(child.id);
              }
              if (child.attribute('desc').length()>0){
              tmpDesc = child.@desc
              } else if (child.child('desc')){
              tmpDesc = child.desc;
              }

              childObj = new Object();
              childObj.id=tmpID;
              childObj.desc = tmpDesc;
              break;
              }
              resultAC.addItem(childObj);
              }
              return resultAC;
              }

              I can't write recursive code off the top of my head--I find it takes me a
              while to get me into the mental state where it makes any sense at all.
              That's why I had to look it up :-).

              HTH;

              Amy


              • 4. Re: amfphp and flex - parsing a struct
                PaulBHartzog Level 1
                thanks all
                I've been working on this all day and ultimately the problem boiled down to not knowing either the depth or the fieldnames in the array.

                Kinda hard to inspect something you know nothing about the structure of, alas.

                Nevertheless, I DID eventually get the things I need into the DataGrid but only because I discovered how deep the struct is. If it were indeterminate, I would need to use recursion to exhaustively iterate through the elements.