Mike
Mike

Reputation: 1768

Calling a method in custom XML class returns null

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>

Upvotes: 0

Views: 182

Answers (1)

Taurayi
Taurayi

Reputation: 3207

Try this instead:

Main.as(document class):

package 
{
    import com.example.CourseXML;
    import flash.display.Sprite;
    import flash.events.Event;

    public class Main extends Sprite 
    {
        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);

        }// end function

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);

            var courseXml:CourseXML = new CourseXML("xml/course.xml");
            courseXml.addEventListener(CourseXML.LOAD_COMPLETE, onCourseXmlLoadComplete);
            trace(courseXml.title)// output: null

        }// end function

        private function onCourseXmlLoadComplete(e:Event):void
        {
            var courseXml:CourseXML = CourseXML(e.target);
            trace(courseXml.title) // output: Test Course

        }// end function

    }// end class

}// end package

CouseXML.as:

package com.example 
{
    import flash.events.Event;
    import flash.events.EventDispatcher;
    import flash.net.URLLoader;
    import flash.net.URLRequest;

    public class CourseXML extends EventDispatcher
    {
        public static const LOAD_COMPLETE:String = "loadComplete";

        private var _urlLoader:URLLoader;
        private var _xml:XML;

        public function get title():String 
        {
            var title:String;

            try 
            {
                title = _xml.@title; 
            }
            catch (e:TypeError)
            {
                title = null;

            }// end catch

            return title;

        }// end function

        public function CourseXML(url:String) 
        {
            _urlLoader = new URLLoader();
            _urlLoader.addEventListener(Event.COMPLETE, onUrlLoaderComplete);
            _urlLoader.load(new URLRequest(url));

        }// end function

        private function onUrlLoaderComplete(e:Event):void
        {
            _xml = XML(URLLoader(e.target).data);

            dispatchEvent(new Event(CourseXML.LOAD_COMPLETE));

        }// end function

    }// end class

}// end package

If your wondering about the try and catch in the CourseXML object's title() getter method, I put that there so if you try and access any of the _xml property's members before the xml file is loaded and assigned to it, you don't get a nasty TypeError.

[UPDATE]

Upon second thought, the try and catch was unnecessary, it would have been easier to use a conditional statement like the following:

public function get title():String 
{
    return (_xml) ? _xml.@title : null;

}// end function

Upvotes: 1

Related Questions