2 Replies Latest reply on Nov 19, 2008 5:52 PM by *gsb*

    Xml from php (mysql) into Array in AS question

    jvankuijk
      Hey all,
      I've been searching a while now how to fix my problem. I have an xml file returned from a php file with data populated from my MySQL database. I approach this data via the HttpService function in Flex. The xml output looks like this:
      (see attached code)

      Ok so that should be fine.
      Now in my Flex project I have 2 AS classes, called Klant (customer) and Klanten (customers). I'll post the code of both classes here:
      KLANT:
      (see attached code)

      KLANTEN:
      (see attached code)

      My purpose is to put the dat in the Xml file (Klant1,Klant2,Klant3,...) into a KlantenArray. I really have no idea how to do this, I tried the following:
      (see attached code)

      And here's my HTTPService:
      (see attached code)
      Any help is greatly appreciated!
      Thanks in advance,

      Jordy van Kuijk
        • 1. Re: Xml from php (mysql) into Array in AS question
          Chaos7703
          I'm not sure about Flex 3, I haven't had time to relearn/rebuild what I only JUST learned & built when Flex 3 came out, but I don't imagine that this part has changed. Try this, I hope that it will help...

          import klant (if needed???)

          [Bindable] private var klantAry:Array = new Array();
          [Bindable] private var Lijst:ArrayCollection;

          private function getKlanten():void{
          getKlanten.send();
          getKlanten.addEventListener(ResultEvent.RESULT,buildKlanten);
          }

          private function buildKlanten(e:ResultEvent):void{
          var inAry:Boolean;
          for each(var x:XML in e.result..klant){
          inAry = false;
          var nextKlant:klant = new klant(x.naam, x.firmanaam);
          for each(var aryKlant:klant in klantAry){
          if( !aryKlant.equals(nextKl) ){
          inAry = false; continue;
          } else {
          inAry = true; break;
          }
          }
          if(!inAry){klantAry.push(kl);
          }
          (optional, I'm not sure if you would want an ArrayCollection)
          Lijst = new ArrayCollection(klantAry)
          }

          So this is my approach to your problem, my application does it with about 12 Db tables and I use the ArrayCollections as DataProviders for all sorts of things. I would recommend using the DISTINCT operator in your SQL to ensure that each node of your XML output is unique.
          (E.g. "SELECT DISTINCT naam, firmanaam FROM klantenTbl") This would allow you to drop the whole duplication test, i.e.:

          for each(var aryKlant:klant in klantAry){
          if( !aryKlant.equals(nextKl) ){
          inAry = false; continue;
          } else {
          inAry = true; break;
          }
          }

          • 2. Re: Xml from php (mysql) into Array in AS question
            *gsb*

            Well, I think several things are wrong.

            1) You code above has some typos in it.
            2) Your data set from your server is not "valid" XML, it has invalid characters in the email field at least.

            You need to wrap those text fields in 'CDATA' tags like:
            <email><![CDATA[ me@Home.com ]]> </email>

            It does not read into your Flex as it is.

            3) If your data is coming as XML, why not use it as such instead of converting it to an array?


            I have added code below to show you how you might do that.
            The example also contains code to make the records unique by the 'naam' field although I agree with Chaos7703 above in that it should be included in your original db query.