Skip to main content
Known Participant
August 9, 2011
Question

reading xml element outside onLoad method

  • August 9, 2011
  • 3 replies
  • 914 views

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>

This topic has been closed for replies.

3 replies

kglad
Community Expert
Community Expert
August 9, 2011

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.

relaxatraja
Inspiring
August 10, 2011

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

Known Participant
August 10, 2011

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;
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);
}

}

kglad
Community Expert
Community Expert
August 9, 2011

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.

relaxatraja
Inspiring
August 9, 2011

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 .

relaxatraja
Inspiring
August 9, 2011

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 )
}