AS3 FileStream附加XML节点

时间:2015-09-14 00:23:06

标签: xml actionscript-3 air

嗨我想通过文件流追加xml节点但不知道如何在flash中使用filestream启动即时通讯代码使其易于理解 xml文件是

<?xml version="1.0" encoding="UTF-8" ?>
<Sounds>
    <Value bez="falconp" id="0">
        <fad>falconpunch.swf.mp3</fad>
    </Value>    
</Sounds>

现在,当我选择一个mp3时,我想添加一个带有fad的新值节点,属性id和bez(bez表示文件名只有几个字符长度)

继承人使用的代码:

public class SoundboardMain
    {
        public var gui:Soundboard;
//      public var req:URLRequest=new URLRequest(string1);
        public var sound:Sound;
        public var xml:XML=new XML();
        public var loader:URLLoader=new URLLoader();
        public var file:File=new File();
        public var daten:FileStream=new FileStream();
        public var expl:File;
        public var fileStream:FileStream = new FileStream(); 
        public var bar:ProgressBar=new ProgressBar();
        public function SoundboardMain(gui:Soundboard)
        {
            this.gui=gui;

            file = File.documentsDirectory.resolvePath("Sounds.xml"); 
            loader.addEventListener(Event.COMPLETE, komp);
            fileStream.open(file, FileMode.WRITE); 
//          xml = XML(fileStream.readUTFBytes(fileStream.bytesAvailable)); 
            loader.load(new URLRequest("Sounds.xml"));
//          fileStream.writeUTFBytes("HELLO");
//          fileStream.close();
            trace(xml);
            loader.addEventListener(Event.COMPLETE, komp);
            loader.addEventListener(IOErrorEvent.IO_ERROR ,Error);



        }
        public function Error(event:IOErrorEvent):void{
            trace("Fehler:! "+event.text);
        }
        public function komp(event:Event):void{         
            trace("1");
            xml=XML(loader.data);
            gui.buttonshin();
        }
        public function custom(event:MouseEvent):void
        {
        expl=new File();
        expl.addEventListener(Event.SELECT, onSelec);
        var swfTypeFilter:FileFilter = new FileFilter("Mp3 Dateien","*.mp3;");
        expl.browse([swfTypeFilter]);

        }
        public function onSelec(event:Event):void{
        expl.addEventListener(Event.COMPLETE, onCompl);
        expl.addEventListener(ProgressEvent.PROGRESS, prog);
        expl.addEventListener(IOErrorEvent.IO_ERROR, onFileLoadError);

        expl.load()
        trace(expl.name);
        fileStream.writeUTFBytes(loader.data);
        bar.visible=true;
        bar.mode=ProgressBarMode.MANUAL;
        bar.minimum=0;
        bar.maximum=100;

        gui.start.addChild(bar);
        }
        public function prog(event:ProgressEvent):void{
        var percentLoaded:Number=event.bytesLoaded/event.bytesTotal*100;
        bar.setProgress(percentLoaded, 100);
        }
        public function onFileLoadError(event:IOErrorEvent):void{
            bar.visible=false;
            expl.removeEventListener(Event.COMPLETE, onCompl);
            expl.removeEventListener(IOErrorEvent.IO_ERROR, onFileLoadError);
            expl.removeEventListener(ProgressEvent.PROGRESS, prog); 
            trace("File load error");
        }
//      private function saveFile():void 
//      {
//          var writeStream:FileStream = new FileStream();
//          /*
//          * FileMode.WRITE will truncate (empty) the file on open.
//          * FileMode.APPEND will open the file "as is" and write in the end of it.
//          */
//          writeStream.open(expl, FileMode.WRITE);
//          writeStream.writeBytes(data); // Any other write method can be used
//          writeStream.close();
//      }
        public function onCompl(event:Event):void
        {

            var ur:URLRequest=new URLRequest(expl.name);
//          trace(event.target.nativePath);
            var string1n:String=new String(event.target.name.substr(0,event.target.nativePath.length-4));
//          trace(string1n);
//          daten.writeUTFBytes("Hekki");
//          var newxml:XML=xml.appendChild("TEST");
//          f.save(newxml,"Sounds.xml");
//          var fs : FileStream = new FileStream();
//          var targetFile : File = File(
//          fs.open(targetFile, FileMode.WRITE);
//          fs.writeUTF("test");
//          fs.close();
////            var ba:ByteArray=new ByteArray();
//          trace(xml);
//          
        }
        public function mp3load(event:Event):void{

        }

        public function image1(event:MouseEvent):void{
            var reqe:URLRequest=new URLRequest("https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=philipp%2elippold%40yahoo%2ede&lc=DE&item_name=Solidtron&no_note=0&currency_code=EUR&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHostedGuest");
//          navigateToURL(reqe);
        }
    }
}

例如,如果我添加和mp3称为funk.mp3 bez = funk xml应该看起来像

  <?xml version="1.0" encoding="UTF-8" ?>
    <Sounds>
        <Value bez="falconp" id="0">
            <fad>falconpunch.swf.mp3</fad>
        </Value>    
        <Value bez="funk" id="1">
            <fad>funk.mp3</fad>
        </Value>
    </Sounds>

0 个答案:

没有答案