使用actionscript文档类追踪xml

时间:2013-12-30 17:48:56

标签: xml actionscript-3 flash

文档类称为ExamXML。我想知道如何在给定文件中加载xml并找出其内容。我是XML的新手,所以我经常遇到错误。

<programme title="Fun Coding" level="8">    
  <module title="Favourite Class" code="STFU" year="3">
    <lecturer>          
      <firstname>Nigel</firstname>      
      <lastname>Douglas</lastname>  
      <email>nigeldouglasweb@gmail.com</email>      
    </lecturer>         
    <content desc="Revision">ActionScript 3 Revision</content>  
    <content desc="Video">Video in ActionScript 3</content>    
    <content desc="XML">Working wth XML in ActionScript 3</content> 
  </module>     

<module title="Intermediate Server-Side Prog" code="ISSP" year="3"> 
    <lecturer>          
      <firstname>Barack</firstname>     
      <lastname>Obama</lastname>        
      <email>bman.whitehouse@gmail.com</email>      
    </lecturer>         
    <content desc="Revision">PHP Revision</content>         
    <content desc="Sessions">PHP Sessions and Cookies</content>         
    <content desc="Files">Working wth files in PHP</content>        
    <content desc="XML">Working wth XML in PHP</content> 
  </module> 
</programme>

1 个答案:

答案 0 :(得分:0)

我希望这会有所帮助:

package 
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.net.URLLoader;
    import flash.net.URLRequest;

    public class ExamXML extends Sprite
    {
        private var someXML:XML;
        /*
         * Create a variable that can hold my TraceWindow object. The class
         * is created by first converting a textfield and a 
         * UIScrollBar to a symbol. Then a traceIt method was added to the class.
         */
        private var tw:TraceWindow;

        public function ExamXML():void
        {
            tw = new TraceWindow();
            tw.width = this.width - 20;
            tw.height = this.height - 20;
            tw.x = 10;
            tw.y = 10;
            this.addChild(tw);
            initXML();
            loadXMLFromFile();
            tw.traceIt("----------------", "XML Loaded from a file");
            traceXML();
        }
        public function initXML():void
        {
            // Note that the XMl does not need (and should not be) wrapped in
            // quotes (")
            someXML = 
                                    <programme title="Fun Coding" level="8">
                                            <module title="Favourite Class" code="STFU" year="3">
                                                    <lecturer>
                                                            <firstname>Nigel</firstname>
                                                            <lastname>Douglas</lastname>
                                                            <email>nigeldouglasweb@gmail.com</email>
                                                    </lecturer>
                                                    <content desc="Revision">ActionScript 3 Revision</content>
                                                    <content desc="Video">Working with video in ActionScript 3</content>
                                                    <content desc="XML">Working wth XML in ActionScript 3</content>
                                            </module>
                                            <module title="Intermediate Server-Side Prog" code="ISSP" year="3">
                                                    <lecturer>
                                                            <firstname>Barack</firstname>
                                                            <lastname>Obama</lastname>
                                                            <email>bman.whitehouse@gmail.com</email>
                                                    </lecturer>
                                                    <content desc="Revision">PHP Revision</content>
                                                    <content desc="Sessions">PHP Sessions and Cookies</content>
                                                    <content desc="Files">Working wth files in PHP</content>
                                                    <content desc="XML">Working wth XML in PHP</content>
                                            </module>
                                    </programme>;
        }
        public function traceXML():void
        {
            tw.traceIt(someXML, "Modules");
        }
        public function loadXMLFromFile():void
        {
            var xmlRequest:URLRequest = new URLRequest("Modules.xml");
            var xmlLoader:URLLoader = new URLLoader();
            xmlLoader.load(xmlRequest);
            xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);
        }

        public function xmlLoaded(theEvent:Event)
        {
            someXML = new XML(theEvent.target.data);
        }
    }
}

TraceWindow Class

package 
{
    import flash.display.MovieClip;
    import flash.text.TextFormat;

    /* 
     *  This class was created as follows:
     *      - Drew a Textarea on the stage with an instance
     *        name of traceTxt
     *      - Add a UIScrollBar to the stage and "linked" it
     *        to the Textarea by setting the scrollTargetName
     *        property of the UIScrollBar
     *
     *  Then select the Textarea and UIScrollBar instances on the
     *  stage and convert them to a symbol. Call the symbol
     *  TraceWindow and check "export for actionscript". Then
     *  create a class called TraceWindow, which is this class.
     *
     *  The only reason I have done this is to demonstrate to you how 
     *  you can create a symbol and add code to the "symbols class".
     */
    public class TraceWindow extends MovieClip
    {

        protected var txtFormat:TextFormat;

        public function TraceWindow()
        {
            // Create a TextFormat object and set its color to red.
            txtFormat = new TextFormat();
            txtFormat.color = 0xFF0000;
        }


        public function traceIt(obj:Object, title:String = "------"):void
        {

            var startIndex = this.traceTxt.length;
            var endIndex = startIndex + title.length + 1;
            this.traceTxt.appendText(title + ":\n");


            this.traceTxt.setTextFormat(txtFormat, startIndex, endIndex);


            this.traceTxt.appendText(obj.toString() + "\n\n");
        }
    }

}

这个答案很大程度上取决于John Hannafin的工作。 对于我的投资组合,请访问http://atondigi.com/