Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML data in the global scope?

Status
Not open for further replies.

BeyondTheBlue

Programmer
May 27, 2007
7
US
I'm working on a small project that will act as a sort of Codex, or reference guide. The structure of the movie is kindof like a DVD, with 3 sections, each section with an associated list of chapters. I wanted to be able to put the text information, like the chapter content, header, etc, in an XML file to make it easier to edit and add chapters later. I've used XML before in some limited applications, but I'm doing it differently now than I have before.

What I want is to dump the contents of the XML file into an array that's accessable at the _root level. I can access it from within the xmlData.onLoad function, but I can't seem to figure out how to access in anything that's not a child-function of that event.

I dont' understand why the following code doesn't work:

Code:
function loadXML(loaded) {
	if (loaded) {
		S1chapHeader = [];
		S1chapContent = [];
		xmlSect1Node = this.childNodes[0];
		total = xmlSect1Node.childNodes.length;
		for (i=0; i<total; i++) {
			S1chapHeader[i] = xmlSect1Node.childNodes[i].attributes.chapHeader;
			S1chapContent[i] = xmlSect1Node.childNodes[i].attributes.chapContent;
		}
		trace(this.childNodes[0].childNodes[0].attributes.chapHeader);
	} 
}
xmlData = new XML();
xmlData.ignoreWhite = true;
xmlData.onLoad = loadXML;
xmlData.load("drift_content.xml");

trace(xmlData.childNodes[0].childNodes[0].attributes.chapHeader);

the trace()command from withing the .onLoad event reports the correct data, but the last line, which is not in the .onLoad event, returns 'undefined'.

Even if I declare _global, _root, or _level0 variables, the .onLoad event handler can't access them.

What's my solution? How can I get access to the XML data in the global scope?
 
Ok, I've got it working.

I have no idea what the deal is, tho. When I declare the variables for the XML structure and the arrays to hold the data in the onLoad even handler, they are NOT accessable from the same frame, but if I move on to another frame (which is why I needed to be able to use global variables) I can read information from the arrays without issue. [neutral]

This doesn't make any sense to me, but I'm going to roll with it for now because I'm on a deadline.
 
When you trace() xmlData is not yet loaded with XML data. Flash does not wait the loading to be finished before moving onto the next line of code ("asynchronous").

Your solution "works" because by the time the play-head moves to another frame the XML has finished loading. But this is just by pure chance and you cannot rely on it. If your XML is big the data takes time to load, and your solution would fail because the data may not be ready on the next frame....!

Kenneth Kawamoto
 
Durrr. That makes perfect sense.

I suppose in this case that the solution would be to make sure that the data has been fully loaded by running through a pre-loader loop and THEN try to make use of the arrays.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top