11 Replies Latest reply on Aug 11, 2011 1:51 AM by relaxatraja

    reading xml element outside onLoad method

    Action Script 3

      i want to read xml element outside onLoad method but i m unable to read can u plz give idea to solve this problem.................i m using this code..

       


      var linkData:XML = new XML();
      var linkXData:XMLNode;
      var serverName:XMLNode;
      var projectName:XMLNode;
      linkData.ignoreWhite = true;
      linkData.load("server.xml");

      linkData.onLoad = function(success:Boolean)
      {
      if(success)
      {
        trace("Link data loaded");
        serverName = linkData.firstChild.childNodes[0].firstChild;
        projectName = linkData.firstChild.childNodes[1].firstChild;
        linkXData = linkData.firstChild;
        trace("in success"+linkXData.childNodes.length)
      }
      else
      {
        trace("Failed to load link data");
      }
      };

       

      trace("ServerName="+ServerName)

      trace("projectName ="+projectName )

       

       

      this is giving ServerName=undefined

                          projectName =undefined

       

       

      my xml is

      <?xml version='1.0' encoding='UTF-8'?>

      <data>

       

      <servername>hyddtl900411</servername>

      <projectname>CCBCPC</projectname>

      </data>

        • 1. Re: reading xml element outside onLoad method
          relaxatraja Level 5

          Solution is simple:

           

          you should wait for the xml to load, give a test by using the below code, you come to know the clue

           

          id=setInterval(testvalue,1000);

          function testvalue():Void{
              trace("ServerName="+serverName)
              trace("projectName ="+projectName )
          }


          • 2. Re: reading xml element outside onLoad method
            kglad Adobe Community Professional & MVP

            you'll still need to wait for it to load and you'll still need to guarantee you won't try and use any xml data until that onLoad() method has been called.

             

            that said, you can enable/display a "continue" button in your onLoad() method or you can call another function from your onLoad() method.  but don't start a loop like relaxa showed.  that's poor coding, at best and when done like relaxa showed, it's terrible coding.

            • 3. Re: reading xml element outside onLoad method
              relaxatraja Level 5

              Even he knows not to use that code. I dont want him to use the code, The code shows just the result of the xml value (for testing) so that he know only the value will be retained after xml load complete. but he understand Mr.kglad as the value still there .

               

              I meant there as "give a test", I think you dont know how to read that .

              • 4. Re: reading xml element outside onLoad method
                kglad Adobe Community Professional & MVP

                you'll still need to wait for it  to load and you'll still need to guarantee you won't try and use any xml  data until that onLoad() method has been called.

                 

                that  said, you can enable/display a "continue" button in your onLoad() method  or you can call another function from your onLoad() method.  but don't  start a loop like relaxa showed.  that's poor coding, at best and when  done like relaxa showed, it's terrible coding.

                • 5. Re: reading xml element outside onLoad method
                  relaxatraja Level 5

                  I thought you dont know how to read! but now knows you dont have the eyes to see. your post duplicated not only here, wherever you dont like my post continue as long as you wish

                  • 6. Re: reading xml element outside onLoad method
                    Action Script 3 Level 1

                    hi

                     

                    i am trying like that..............in that i want 2 use servername and projectname in navigation bar but sometimes it is working somtimes it is not working......

                    i think due to loading of xml.can u give me some idea so that i can use that variable after loading of xml.

                     

                    var linkData:XML = new XML();
                    var linkXData:XMLNode;
                    var serverName:XMLNode;
                    var projectName:XMLNode;
                    var a;
                    var b;

                    linkData.ignoreWhite = true;
                    linkData.load("server.xml");
                     
                    linkData.onLoad = function(success:Boolean)
                    {
                    if(success)
                    {
                      trace("Link data loaded");
                      serverName = linkData.firstChild.childNodes[0].firstChild;
                      projectName = linkData.firstChild.childNodes[1].firstChild;
                      linkXData = linkData.firstChild;
                      trace("in success"+linkXData.childNodes.length)
                    }
                    else
                    {
                      trace("Failed to load link data");
                    }
                    };

                    id=setInterval(testvalue,1000);
                    function testvalue():Void{
                    a=serverName;
                    b=projectName;
                         trace("ServerName outside="+serverName)
                         trace("projectName outside ="+projectName )
                    setserver(serverName);

                    clearInterval(id);

                      }
                      var val;
                    function setserver(val){
                    trace("val="+val)
                    init1(val);
                    }

                     

                     

                    in init1() function...................................

                     


                    function init1(val){
                    trace("val in init1="+val)
                    trace("///////////////////////init1//////////////////////")
                    trace("ServerName in init1="+serverName)
                    trace("projectName in init1 ="+projectName )
                    /*var localhost="hyddtl900411";
                    var projectName="CCBCPC";*/

                    var localhost=val;
                    var projectName="CCBCPC";
                    trace("localhost="+localhost)
                    var links:Array = new Array();
                    trace("link="+links[0])
                    links[0] = "http://"+localhost+":8080/"+projectName+"/web/productOverview.html";
                    links[1] = "http://"+localhost+":8080/"+projectName+"/web/Training.html";
                    links[2] = "http://"+localhost+":8080/"+projectName+"/web/a.html";
                    links[3] = "http://"+localhost+":8080/"+projectName+"/web/Case_studies.html";
                    links[4] = "http://"+localhost+":8080/"+projectName+"/web/Team.html";
                    links[5] = "http://"+localhost+":8080/"+projectName+"/web/offering.html";

                    for(var i:Number = 0; i < links.length; i++)
                    {
                    this["btn_" + i].pageRef = links[i];
                    this["btn_" + i].onPress = function()
                    {
                      getURL(this.pageRef);
                    }
                    }
                    //highlight current button
                    if(_root.activeBtn != undefined)
                    {
                    this["btn_" + _root.activeBtn].enabled = false;
                    this["btn_" + _root.activeBtn].useHandCursor = false;
                    this["btn_" + _root.activeBtn].gotoAndStop(4);
                    }

                    }

                    • 7. Re: reading xml element outside onLoad method
                      relaxatraja Level 5

                      If you want to retrieve the value outside of the xml, you must wait for the completion of the xml to be loaded. So the method you should follow is look like below:

                       

                      linkData.onLoad = function(success:Boolean)
                      {
                      if(success)
                      {
                        trace("Link data loaded");
                        serverName = linkData.firstChild.childNodes[0].firstChild;
                        projectName = linkData.firstChild.childNodes[1].firstChild;
                        linkXData = linkData.firstChild;
                        trace("in success" + linkXData.childNodes.length)
                        getData();
                      }
                      else
                      {
                        trace("Failed to load link data");
                      }
                      };

                      function getData():Void{
                          a=serverName;
                          b=projectName;
                           trace("ServerName outside="+serverName)
                           trace("projectName outside ="+projectName )
                      setserver(serverName);
                      }

                      Now there is no need of setInterval because I give the option for testing the xml value. So the above code shows the completion of the xml load and use getData function which is defined outside of the function.

                      1 person found this helpful
                      • 8. Re: reading xml element outside onLoad method
                        kglad Adobe Community Professional & MVP

                        again, don't use the coding suggested by relaxa.  use something like:

                         


                         

                        var linkData:XML = new XML();
                        var linkXData:XMLNode;
                        var serverName:XMLNode;
                        var projectName:XMLNode;

                        linkData.ignoreWhite = true;
                        linkData.load("server.xml");
                         
                        linkData.onLoad = function(success:Boolean)
                        {
                        if(success)
                        {
                          trace("Link data loaded");
                          serverName = linkData.firstChild.childNodes[0].firstChild;
                          projectName = linkData.firstChild.childNodes[1].firstChild;
                          linkXData = linkData.firstChild;
                          trace("in success"+linkXData.childNodes.length)

                        init1();


                        }
                        else
                        {
                          trace("Failed to load link data");
                        }
                        };

                         


                        function init1(){
                        trace("///////////////////////init1//////////////////////")
                        trace("ServerName in init1="+serverName)
                        trace("projectName in init1 ="+projectName )
                        /*var localhost="hyddtl900411";
                        var projectName="CCBCPC";*/

                        var localhost=val;
                        var projectName="CCBCPC";
                        trace("localhost="+localhost)
                        var links:Array = new Array();
                        trace("link="+links[0])
                        links[0] = "http://"+localhost+":8080/"+projectName+"/web/productOverview.html";
                        links[1] = "http://"+localhost+":8080/"+projectName+"/web/Training.html";
                        links[2] = "http://"+localhost+":8080/"+projectName+"/web/a.html";
                        links[3] = "http://"+localhost+":8080/"+projectName+"/web/Case_studies.html";
                        links[4] = "http://"+localhost+":8080/"+projectName+"/web/Team.html";
                        links[5] = "http://"+localhost+":8080/"+projectName+"/web/offering.html";

                        for(var i:Number = 0; i < links.length; i++)
                        {
                        this["btn_" + i].pageRef = links[i];
                        this["btn_" + i].onPress = function()
                        {
                          getURL(this.pageRef);
                        }
                        }
                        //highlight current button
                        if(_root.activeBtn != undefined)
                        {
                        this["btn_" + _root.activeBtn].enabled = false;
                        this["btn_" + _root.activeBtn].useHandCursor = false;
                        this["btn_" + _root.activeBtn].gotoAndStop(4);
                        }

                        }

                        1 person found this helpful
                        • 9. Re: reading xml element outside onLoad method
                          relaxatraja Level 5

                          Give solution, but dont blame others. Everybody here to help the people. no offence. Try to change your thought on your mind. . What you given above is what I suggested. Changing "getData" to "init" will make a difference?. You thinking the thread starter is a new born child?

                           

                          You'r trying from the begining to get me lost from the adobe forum. NEVER HAPPEN

                          • 10. Re: reading xml element outside onLoad method
                            Action Script 3 Level 1

                            Thank u both its working now.................