8 Replies Latest reply on Dec 8, 2009 9:15 PM by cbabilotte

    variable return null value.

    cbabilotte

      I have created an action script class to load an xml file and I would like to get the content of the xml within the flex mxml script. It appears that if I put a debug statement in the mxml file the return value is null. How can I fix this so that the content of the xml file is passed from the action script to the mxml file? thanks.

       

      Here is the debug statement.

       

      [SWF] xmlwork.swf - 1,195,064 bytes after decompression
      xml in init function : null

       

      Below is the code for the action script file

       

      package Widget
      {
          // import needed classes 
          import mx.rpc.events.FaultEvent;
          import mx.rpc.events.ResultEvent;
          import mx.rpc.http.HTTPService;
             
         
          public class xmlLoader
          {   
             
              // our xml object
              public var xml:XML;
              public var loader:HTTPService = new HTTPService;
              
              public function xmlLoader()
              {
                  loader.useProxy = false;
                  loader.addEventListener(ResultEvent.RESULT, xmlLoaded);
                  loader.addEventListener(FaultEvent.FAULT,xmlRequest_Error);
              }
             
              public function getxmlContent(xmlfile:String):void
              {   
                  loader.url = xmlfile;
                  loader.resultFormat = "e4x";
                  loader.send();
              }
              public function xmlLoaded(event:ResultEvent):void
              {
                  xml = event.result as XML;
              }

              public function xmlRequest_Error(event:FaultEvent):void
              {
                  trace(event.fault.message, "XML file doesn't not exists");
              }
          }
      }

       

      Below is the mxml code

       

      <mx:Script>
              <![CDATA[
                  import flash.system.Capabilities;
                  import flash.utils.Timer; 
                  import mx.controls.Alert;
                  import mx.core.UIComponent;
                  import mx.events.CloseEvent;
                  import mx.rpc.events.FaultEvent;
                  import mx.rpc.events.ResultEvent;
                  import mx.validators.Validator;
                  import mx.validators.ZipCodeValidator;
                 
                  // configuration parsing
                  import Widget.xmlLoader;
                 
                  private var configurationFile:String = "config/config.xml";

       

                  private function init():void
                  {   
                       var configData:xmlLoader = new xmlLoader();
                      configData.getxmlContent(configurationFile);

       

                      trace("xml in init function : "+configData.xml);
         
                  }
          ]]>

        • 1. Re: variable return null value.
          Gregory Lafrance Level 6

          Flex HTTPService is asynchronous, so you can't call trace right after configData.getxmlContent() because the data may not yet be available.

           

          You are better off doing your trace in the AS class.

           

          If this post answered your question or helped, please mark it as such.

          1 person found this helpful
          • 2. Re: variable return null value.
            cbabilotte Level 1

            thanks for your reply.

             

            So if HTTPService is asynchronous, then how can I make the xml content available right in the mxml code? My idea was to create a AS script as a loader and get the content in mxml file so I can manipulate it as I might have quite a few xml file to read. So I was thinking to have something like this in my mxml code:

             

            private var ZipCode:String;

            private var xmlcontent:XML;

             

            configData.getxmlContent(configurationFile);

            xmlcontent = configData.xml;

             

            ZipCode = xmlcontent.tag.zipcode;

             

            ....

             

             

            but if trace(configData.xml) is showing my null value then I am not sure how I can access the xml content from the mxml script. thanks.

            • 3. Re: variable return null value.
              salem_b_a Level 2

              Hi,

               

              i got an idea that may help,

               

              when  handling the result in the class dispatch a custom event (LOADED for example)

               

               

              on the mxml file make the configData a property of the mxml file not a local var,

              and add a listener to that event.

               

              //addEventListener(loadedEvent, loadedHandler)

               

              some googling for custom events would help a lot

              Salem

              • 4. Re: variable return null value.
                cbabilotte Level 1

                Salem,

                 

                thanks for your suggestion.I have another question. Based on the function xmlComplete it appears that the ZipCode variable (which I think is declared as global) has a null value in the init function but actually return something in the xmlComplete function. How can I make available the return value of ZipCode globally not just in xmlComplete function?

                 

                Debug output:

                 

                init: ZipCode: null
                xmlComplete: zip code: 11111

                 

                 

                mxml code:

                 

                         
                           import flash.system.Capabilities;
                            import flash.utils.Timer; 
                            import mx.controls.Alert;
                            import mx.core.UIComponent;
                            import mx.events.CloseEvent;
                            import mx.rpc.events.FaultEvent;
                            import mx.rpc.events.ResultEvent;
                            import mx.validators.Validator;
                            import mx.validators.ZipCodeValidator;
                           
                            // configuration parsing
                            import Widget.xmlLoader;
                           
                            private var configurationFile:String = "config/config.xml";
                            private var configData:xmlLoader;
                            private var xmldata:XML;
                            private var ZipCode:String;

                 


                            private function init():void
                            {   
                                configData = new xmlLoader(configurationFile);
                                configData.addEventListener(Event.COMPLETE,function(evt:Event){xmlComplete(evt,configData )});
                               
                               
                                trace("init: ZipCode: "+ZipCode);  

                 

                            }
                           
                            private function xmlComplete (evt:Event,configData:xmlLoader):void
                            {   
                                xmldata = configData.xmlContent();
                                ZipCode = xmldata.widget.zip;
                                trace("xmlComplete: zip code: "+ZipCode);
                            }

                • 5. Re: variable return null value.
                  salem_b_a Level 2

                  hi cbabilotte,

                  first, can you tell me why all you functions are declared private?

                   

                  i didn't understand what do you really mean by 'make available the return value of ZipCode globally' .

                   

                  if i'm not out,i'll suggest two halves solution

                   

                  if you want to use the zipCode in the same mxml, it is ready after you receive the result event.

                  i mean after calling the init() method (probably on the creationComplete event), your zipCode would be available for use in other mxml, you have just to check if it's null.

                   

                  else,if you wanna use zipCde outside the mxml, you have just to add a public getter

                   

                   

                  Hope this helps,

                  Salem

                  1 person found this helpful
                  • 6. Re: variable return null value.
                    cbabilotte Level 1

                    Salem,

                     

                    Here is my idea behind the code. Make an AS to load the xml as I would like to load more than 1 xml file. Pass the content of the xml back to the mxml script to parse it. So what I would like to see if the following:

                     

                       private ZipCode:String;

                     

                       public init():void

                       {

                               // load the xml file

                               // add event listener (xmlComplete)

                               // parsexml()

                       }

                     

                     

                       public xmlComplete(e:event):void

                       {

                           // get the xml content

                     

                       }

                     

                       public parsexml():void

                       {

                          // do some parsing based on the xml received in xmlComplete

                          // get the zip code for example and use the zip code for other fetch (IE: Lat/Long, etc......)

                       }

                     

                    Hence in my case if I make a global variable Zipcode as soon as I get the xmlComplete done I should be able to retrieve the value of ZipCode in the parsexml function (at least that is the idea). Now I guess I am not understanding how the event listener is working. Thanks for all your help.

                    • 7. Re: variable return null value.
                      pauland Level 4

                      Move the parsexml() call into the xmlComplete function.

                       

                      In your existing code parsexml() will be called before the XML has loaded.

                       

                      Paul

                      • 8. Re: variable return null value.
                        cbabilotte Level 1

                        Thanks I got it to work.