Wednesday, February 08, 2006

Use XML content in Flash8


In this small tutorial I am going to show you how to use XML content in flash.

Step1: Creating your own XML file
Step2: Creating the action-script code in Flash
Step3: Place the content in a dynamic text box

Creating you own XML file
You can create an XML file in notepad or any other text editor, notepad for example.

In notepad place the following code.
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<mytext>
<message>
<subject>This is a tutorial about XML</subject>
<text>Here you can place you own text</text>
</message>
</mytext>
now save the file with the name mytext and don't forget the extension .xml else you will create a txt file instead of an XML file.

Creating the action script code in Flash
No we are going to create a new flash document. Open flash, en create a new file before you are going to start and add action-script to the flash document save the file in the same directory where you saved the XML file.


Now go to the first frame in the time line, after that select the text box tool from the tool menu on the left.

Draw two text box fields on the stage, one will be for the subject and the other will be for the text.
You can make the one for the text higher so more text will appear at once.

Select the first text box and go to properties as shown in the image below, you can find the properties at the bottom of the screen.

In the properties tab, you can select a type for the text box, static text,dynamic text and input text. Select dynamic text from the drop down menu. Under the drop down menu, you have an input field, click on it a type subject. Now we have gave the subject text box the name subject.
Do exactly the same thing for the text box for the text and give it the name mytext.

In the middle of the properties window, you can see that the text box is set to Single line change that into multiline.



Place the content in a dynamic text box
The only thing that is left, is the code in Flash. Again select the first frame in the time line, hit the F9 key on the keyboard. the action script window should appear now.

put in the following code.

xmlData = new XML();
xmlData.ignoreWhite = true;
xmlData.onLoad = loadXML;
xmlData.load("mytext.xml");

function loadXML(loaded) {

if (loaded) {
xmlNode = this.firstChild;
subject = [];
text = [];
total = xmlNode.childNodes.length;
for (i=0; i<total; i++) {
subject[i] = xmlNode.childNodes[i].childNodes[0].firstChild.nodeValue;
text[i] = xmlNode.childNodes[i].childNodes[1].firstChild.nodeValue;
showtext(i);
}

}else{
content = "file not loaded!";
}
}function showtext(i){
_root.subject.text = subject[i];
_root.mytext.text = text[i];
}

stop();

No comments: