Jump to content
  • Advertisement
Sign in to follow this  
eSCHEn

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

This topic is 4183 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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;
			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
Advertisement
Problem solved. I was using the array before xml_loader had fully loaded the file. Added a flag to class to indicate when it was done and triggered it from within the onLoad method.

This link helped a lot.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!