I am trying to call a method that has some xml functionality and It keeps coming up null. What I am trying to do is; I have a page that has four dynamic text fields that get their content from an xml file. I would like to create a method that will output the contents to display in the dynamic field. Maybe my approach is WAY off here, but my supervisor wants all xml related tasks contained in a xml.as file.
Main.as
package classes
{
import flash.display.*;
import flash.events.*;
import classes.Xml; /* my custom class */
public class Main extends MovieClip
{
public function Main():void
{
var xml:Xml = new Xml("menu.xml");
trace(xml.getCourseTitle());
}
}
}
Xml.as
package classes
{
import flash.display.*;
import flash.net.*;
import flash.events.*;
public class Xml extends MovieClip
{
private var xml:XML;
private var loader:URLLoader = new URLLoader();
public function Xml(p:String):void
{
loader.load(new URLRequest(p));
loader.addEventListener(Event.COMPLETE,processXML);
}
public function processXML():void
{
xml = new XML(loader.data)
trace(xml); /* this will trace all xml data in xml file */
}
public function getCourseTitle():String
{
return xml.@title; /* this is supossed to return Test Course */
}
}
}
menu.xml
<?xml version="1.0"?>
<course title="Test Course">
<folder name="Question 1" link="1_1.swf"/>
<folder name="Question 2" link="1_2.swf"/>
<folder name="Question 3" link="1_3.swf"/>
<folder name="Question 4" link="1_4.swf"/>
<folder name="Question 5" link="1_5.swf"/>
<folder name="Question 6" link="1_6.swf"/>
<folder name="Question 7" link="1_7.swf"/>
<folder name="Question 8" link="1_8.swf"/>
<folder name="Question 9" link="1_9.swf"/>
<folder name="Question 10" link="1_10.swf"/>
</course>
Try this instead:
Main.as(document class):
CouseXML.as:
If your wondering about the
tryandcatchin theCourseXMLobject’stitle()getter method, I put that there so if you try and access any of the_xmlproperty’s members before the xml file is loaded and assigned to it, you don’t get a nastyTypeError.[UPDATE]
Upon second thought, the try and catch was unnecessary, it would have been easier to use a conditional statement like the following: