Sign in to follow this  
eSCHEn

[web] Actionscript2 - Load XML data into an array [solved]

Recommended Posts

Hi there. I have a class that has an array as a private property and I'm trying to load an XML file's contents into it. The code for the class is (cut-down example):
class CScreenCredits
{
	private var m_credits_lines:Array;

	function CScreenCredits()
	{
		// create credits array
		this.m_credits_lines = new Array();
	}

	private function AddCreditLine(Text:String, Format:String):Number
	{
		return this.m_credits_lines.push([Text, Format]);
	}

	private function LoadCredits(Filename:String):Boolean
	{
		// clear previous credits
		this.m_credits_lines = [];

		// get copy of this as actionscript gets confused (again *sigh*)
		var this_object = this;

		// setup xml loader
		var xml_loader:XML     = new XML();
		xml_loader.ignoreWhite = true;

		// loader completion function
		xml_loader.onLoad = function(HasLoaded:Boolean)
		{
			if (HasLoaded)
			{
				this_object.ProcessXMLFile(this);
			}
		}

		// do load
		return xml_loader.load(Filename);
	}

	private function ProcessXMLFile(XMLFile:XML):Void
	{
		for (var i=0;i<XMLFile.firstChild.childNodes.length;i++)
		{
			var line_node:XMLNode = XMLFile.firstChild.childNodes[i];
			var new_line:Array    = new Array();

			for (var j in line_node.childNodes)
			{
				var node_type:String  = line_node.childNodes[j].nodeName;
				var node_value:String = line_node.childNodes[j].firstChild.nodeValue;

				if (node_type == "text")
				{
					new_line[0] = node_value;
				}
				else
				{
					if (node_type == "format")
					{
						new_line[1] = node_value;
					}
				}
			}
			// TRACE ADD HERE
			this.AddCreditLine(new_line[0], new_line[1]);
		}
	}

	public function Build():Void
	{
	    trace("before:" + this.m_credits_lines.length);
	
		// load credits text
		this.LoadCredits("./conf/credits.xml");

		trace("after:" + this.m_credits_lines.length);
	}
}

The XML file I'm loading:
<?xml version="1.0" encoding="UTF-8" ?>
<credits>
	<line>
	    <format>bold</format>
		<text>Code</text>
	</line>
	<line>
	    <format>normal</format>
		<text>My Name</text>
	</line>
	<line>
	    <format>bold</format>
		<text>Thanks To</text>
	</line>
	<line>
	    <format>normal</format>
		<text>Another Name</text>
	</line>
</credits>

The code to call the class (again, cut-down):
var my_screen:CScreenCredits = new CScreenCredits();
my_screen.Build();
Although tracing the XML loading (adding a trace at "TRACE ADD HERE") reveals the XML is loading fine. The two traces further down in the Build() method show no change in the array's size (length 0 and length 0 respectively). I've been at this for a while now and it's becoming most annoying. I'm on FlashDevelop 2.02 final if it's of any consequence. Any help would be great. Many thanks. [Edited by - eSCHEn on April 10, 2007 3:03:34 AM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this