# How to swap an xml file when a button is clicked

**URL:** <https://forum.kirupa.com/t/how-to-swap-an-xml-file-when-a-button-is-clicked/324711>\
**Category:** Uncategorized\
**Created:** [September 5, 2011, 3:55am UTC](https://forum.kirupa.com/t/how-to-swap-an-xml-file-when-a-button-is-clicked/324711 "2011-09-05T03:55:10Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![r1chyb](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@r1chyb](https://forum.kirupa.com/u/r1chyb)\
**Post date:** [September 5, 2011, 3:55am UTC](https://forum.kirupa.com/t/how-to-swap-an-xml-file-when-a-button-is-clicked/324711/1 "2011-09-05T03:55:10Z")

</div>

hey everyone, i’m creating a music app where I have arranged different sounds in xml files. I have 16 buttons on stage that play a sound from my xml file. Is there a way to swap the active xml file at the click of a button for one with different sounds? here is my code so far:

var audioArray:Array = new Array();  
var snd:Sound = new Sound();  
var sc:SoundChannel = new SoundChannel();  
var xmlLoader:URLLoader = new URLLoader();  
var xmlData:XML = new XML();

function init():void {

```
xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);
xmlLoader.addEventListener(IOErrorEvent.IO_ERROR, xmlFailed);
xmlLoader.load(new URLRequest("synth1.xml"));

btn1.addEventListener(MouseEvent.CLICK, a);
btn2.addEventListener(MouseEvent.CLICK, b);
btn3.addEventListener(MouseEvent.CLICK, c);
btn4.addEventListener(MouseEvent.CLICK, d);
btn5.addEventListener(MouseEvent.CLICK, e);
btn6.addEventListener(MouseEvent.CLICK, f);
btn7.addEventListener(MouseEvent.CLICK, g);
btn8.addEventListener(MouseEvent.CLICK, h);
btn9.addEventListener(MouseEvent.CLICK, i);
btn10.addEventListener(MouseEvent.CLICK, j);
btn11.addEventListener(MouseEvent.CLICK, k);
btn12.addEventListener(MouseEvent.CLICK, l);
btn13.addEventListener(MouseEvent.CLICK, m);
btn14.addEventListener(MouseEvent.CLICK, n);
btn15.addEventListener(MouseEvent.CLICK, o);
btn16.addEventListener(MouseEvent.CLICK, p);

```

}

init();

function xmlLoaded(e:Event):void {  
parseXML(new XML(e.target.data));  
}

function xmlFailed(e:IOErrorEvent):void {  
trace(“XML Failed”);  
}

function parseXML(xml:XML):void {

```
for (var i:int=0; i&lt;xml.SONG.length(); i++) {
    audioArray.push(xml.SONG*.@URL);
}

```

}

function a(e:MouseEvent):void {  
playSound(1);  
}

function b(e:MouseEvent):void {  
playSound(2);  
}

function c(e:MouseEvent):void {  
playSound(3);  
}  
function d(e:MouseEvent):void {  
playSound(4);  
}

function e(e:MouseEvent):void {  
playSound(5);  
}

function f(e:MouseEvent):void {  
playSound(6);  
}  
function g(e:MouseEvent):void {  
playSound(7);  
}

function h(e:MouseEvent):void {  
playSound(8);  
}

function i(e:MouseEvent):void {  
playSound(9);  
}  
function j(e:MouseEvent):void {  
playSound(10);  
}

function k(e:MouseEvent):void {  
playSound(11);  
}

function l(e:MouseEvent):void {  
playSound(12);  
}  
function m(e:MouseEvent):void {  
playSound(13);  
}  
function n(e:MouseEvent):void {  
playSound(14);  
}

function o(e:MouseEvent):void {  
playSound(15);  
}

function p(e:MouseEvent):void {  
playSound(16);  
}

function playSound(num:Number):void {  
sc.stop();  
snd = new Sound();  
snd.load(new URLRequest(audioArray[num]));  
snd.addEventListener(IOErrorEvent.IO\_ERROR, sndIOError);  
snd.addEventListener(Event.COMPLETE, sndComplete);  
}

function sndIOError(e:IOErrorEvent):void {  
trace(“Error loading sound”);  
}

function sndComplete(e:Event):void {  
sc = snd.play();  
}
