PDA

View Full Version : Setting .xml data to a variable and using in Flash.


XeroXer
08-27-2008, 08:54 AM
Hi there, I am really new to flash and actionscript and after following a tutorial on how to read xml data into actionsscript 3 I solved a piece of the problem.
What I want to do is read the xml data into the actionscript and insert it into a variable that I can then use in my Flash clip.

var xmlLoader:URLLoader = new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE, showXML);
xmlLoader.load(new URLRequest("hittasaljare.xml"));
function showXML(e:Event):void {
XML.ignoreWhitespace = true;
var hittasaljare:XML = new XML(e.target.data);
trace(hittasaljare.saljare.length());
var i:Number;
for (i=0; i < hittasaljare.saljare.length(); i++) {
trace(" Namn: "+ hittasaljare.saljare[i].namn.text());
trace(" Ort: "+ hittasaljare.saljare[i].ort.text());
trace(" E-post: "+ hittasaljare.saljare[i].epost.text());
}
}

What I want is for the namn, ort and epost information from the xml file to be inserted into a variable that I can then use in the flash file.

Sicontis
08-27-2008, 11:29 AM
You should declare some variables at the top of the page to hold the data from the XML page. Since you're looping thru the xml nodes with an iterator(i) then it makes sense to use arrays.

So at the top you declare empty arrays for each


var namnArray:Array = []; // shorthand
var ortArray:Array = [];
var epostArray:Array = [];


Then in your for-loop you put the data into the arrays using the Array.push() method

for (i=0; i < hittasaljare.saljare.length(); i++) {
namnArray.push(hittasaljare.saljare.namn[i]);
ortArray.push(hittasaljare.saljare.ort[i]);
epostArray.push(hittasaljare.saljare.epost[i]);
}

XeroXer
08-27-2008, 01:41 PM
Tried to modify to make it work, but as I said not really my coding knowledge.

Frame 1:
var xmlLoader:URLLoader = new URLLoader();
var delArray:Array = [];
var namnArray:Array = [];
var ortArray:Array = [];
var epostArray:Array = [];
var i:Number;
xmlLoader.addEventListener(Event.COMPLETE, showXML);
xmlLoader.load(new URLRequest("hittasaljare.xml"));
function showXML(e:Event):void {
XML.ignoreWhitespace = true;
var hittasaljare:XML = new XML(e.target.data);
for (i = 0; i < hittasaljare.saljare.length(); i++) {
delArray.push(hittasaljare.saljare.delavlandet[i]);
namnArray.push(hittasaljare.saljare.namn[i]);
ortArray.push(hittasaljare.saljare.ort[i]);
epostArray.push(hittasaljare.saljare.epost[i]);
}
}
stop();

Then I made som buttons to send me to let's say Frame 5:
var thisOut1:String = "";
for (i=0; i < delArray.length(); i++) {
if (delArray[i] == 1) {
thisOut1 = thisOut1 + "<b>" + namnArray[i] + ", " + ortArray[i] + "</b><br><a href='mailto:" + epostArray[i] + "'>" + epostArray[i] + "</a><br><br>";
}
}
theOutput.htmlText = this.thisOut1;
stop();

XeroXer
08-27-2008, 07:08 PM
I solved my problem now, but thought I might post how I did it so people could tell me if there is any better way. Not that it's not good but maybe not reliable.

Frame 1:
var xmlLoader:URLLoader = new URLLoader;
xmlLoader.addEventListener(Event.COMPLETE, showXML);
xmlLoader.load(new URLRequest("hittasaljare.xml"));

var outString1:String = "";
var outString2:String = "";
var outString3:String = "";

function showXML(e:Event):void {
var i:Number;
XML.ignoreWhitespace = true;
var hittasaljare:XML = new XML(e.target.data);
for (i = 0; i < hittasaljare.saljare.length(); i++) {
if (hittasaljare.saljare.delavlandet[i] == 1) {
outString1 = outString1 + "<b>" + hittasaljare.saljare.namn[i] + ", " + hittasaljare.saljare.ort[i] + "</b><br><a href='mailto:" + hittasaljare.saljare.epost[i] + "'>" + hittasaljare.saljare.epost[i] + "</a><br><br>";
} else if (hittasaljare.saljare.delavlandet[i] == 2) {
outString2 = outString2 + "<b>" + hittasaljare.saljare.namn[i] + ", " + hittasaljare.saljare.ort[i] + "</b><br><a href='mailto:" + hittasaljare.saljare.epost[i] + "'>" + hittasaljare.saljare.epost[i] + "</a><br><br>";
} else if (hittasaljare.saljare.delavlandet[i] == 3) {
outString3 = outString3 + "<b>" + hittasaljare.saljare.namn[i] + ", " + hittasaljare.saljare.ort[i] + "</b><br><a href='mailto:" + hittasaljare.saljare.epost[i] + "'>" + hittasaljare.saljare.epost[i] + "</a><br><br>";
}
}
gotoAndStop(2);
}

stop();

Frame 2:
stop();

Then I have three buttons made by this method:
button_1.addEventListener(
MouseEvent.MOUSE_UP,
function(evt:MouseEvent):void {
gotoAndStop(3);
}
);

They go to frame 3, 4 and 5.

I also have a Dynamic textbox that accepts html code in the flash with instance name theOutput.

Frame 3:
theOutput.htmlText = this.outString1;

stop();

Frame 4:
theOutput.htmlText = this.outString2;

stop();

Frame 5:
theOutput.htmlText = this.outString3;

stop();


This is the solution I worked out, if anyone have any pointers feel free to tell me.

Oh yeah!
XML file:
<hittasaljare>
<saljare>
<delavlandet>1</delavlandet>
<namn>Karin Bergsson</namn>
<ort>Västerås</ort>
<epost>karin@example.com</epost>
</saljare>
<saljare>
<delavlandet>1</delavlandet>
<namn>Sven karlsson</namn>
<ort>Halmstad</ort>
<epost>sven@example.com</epost>
</saljare>
<saljare>
<delavlandet>2</delavlandet>
<namn>Arne Thuresson</namn>
<ort>Askersund</ort>
<epost>arne@example.com</epost>
</saljare>
<saljare>
<delavlandet>2</delavlandet>
<namn>Sara Liusoun</namn>
<ort>Stockholm</ort>
<epost>sara@example.com</epost>
</saljare>
<saljare>
<delavlandet>3</delavlandet>
<namn>Lisa Pettersson</namn>
<ort>Hallsberg</ort>
<epost>lisa@example.com</epost>
</saljare>
<saljare>
<delavlandet>3</delavlandet>
<namn>Urban Ohlson</namn>
<ort>Norrköping</ort>
<epost>urban@example.com</epost>
</saljare>
</hittasaljare>