我试图调用一个具有一些xml功能的方法,并且它一直保持为null。我想做的是;我有一个页面,有四个动态文本字段,从xml文件中获取其内容。我想创建一个方法,输出要在动态字段中显示的内容。也许我的方法是在这里,但我的主管想要xml.as文件中包含的所有xml相关任务。
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>
答案 0 :(得分:1)
请改为尝试:
Main.as (文档类):
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
如果您对try
对象的catch
getter方法中的CourseXML
和title()
感到奇怪,那么如果您尝试访问_xml
中的任何一个,我会将其放在那里在加载并分配xml文件之前,属性的成员,你不会得到一个讨厌的TypeError
。
<强> [UPDATE] 强>
第二个想法,try和catch是不必要的,使用如下的条件语句会更容易:
public function get title():String
{
return (_xml) ? _xml.@title : null;
}// end function