 | 
August 15th, 2008, 08:01 AM
| | Newbie | | Join Date: Aug 2008
Posts: 4
| | External XML into ActionScript 3.0 Text Field
I've been trying to implement simple lines of XML into my flash file created with ActionScript 3.0, but things seem not to be working.
All I want to do is to load an external XML file (data.xml), and output the content which are inside the <project text="content to show"></project> tags into a scripted text field.
Here are what I've been doing so far: -
var txtFldPassage:TextField = new TextField();
-
txtFldPassage.multiline = txtFldPassage.wordWrap = true;
-
txtFldPassage.type = TextFieldType.INPUT;
-
txtFldPassage.border = txtFldPassage.background = true;
-
-
var slideNum:Number = 0;
-
var navData:XML;
-
var newnavData:XMLList;
-
var loader:URLLoader = new URLLoader();
-
loader.addEventListener(Event.COMPLETE, onComplete, false, 0, true);
-
loader.addEventListener(IOErrorEvent.IO_ERROR, onIOError, false, 0,
-
true);
-
loader.load(new URLRequest("data.xml"));
-
function onComplete(evt:Event):void {
-
try {
-
navData = new XML(evt.target.data);
-
newnavData = navData.children();
-
trace(newnavData);
-
txtFldPassage.text = newnavData[slideNum].name().toString();
-
loader.removeEventListener(Event.COMPLETE, onComplete);
-
loader.removeEventListener(IOErrorEvent.IO_ERROR,
-
onIOError);
-
} catch (err:Error) {
-
trace("Could not parse loaded content as XML:\n" + err.
-
message);
-
}
-
}
-
function onIOError(evt:IOErrorEvent):void {
-
trace("An error occurred when attempting to load the XML.\n" +
-
evt.text);
-
}
-
-
-
addChild(txtFldPassage);
-
and here's the XML file, data.xml -
<?xml version='1.0' encoding='UTF-8'?>
-
<data>
-
<list1>
-
<project text="When the Tail is Gone" mp3="a1.mp3"/>
-
<project text="What is a /1baby frog called? Where does it /2come from? How does a baby /3frog grow into an adult frog? This is the story of the life cycle of the frog."/>
-
<project text="When it is still winter, frogs lay their eggs. They do not have a /3nest; instead they lay them at the edge of a pond. And they do not lay just one egg; they lay /1hundreds. They look like /2little round balls of /2jelly, each with a black dot in the center. Sometimes, if the pond is small, it can be completely full of jelly. The eggs have to be just under the water surface but not too deep. /2Otherwise, the tiny frogs will die."/>
-
<project text="The /1frog is an amphibian, that is, an /1animal that lives partly in water and partly on land. Unlike frogs, toads live more on land. As an adult, a frog lives on land but it lays its eggs in water. These /3eggs are called frogspawn, and they are the first stage in the amazing metamorphosis, or change, into adult frogs."/>
-
<project text="A few weeks later things have begun to /1develop. Some of the eggs have little tails, and are /3beginning to swim. When they look like this, they are called /4tadpoles. They have little openings on the side of their /4head called gills. They /3breathe underwater through their gills like fish."/>
-
<project text="Next, they grow legs, and use them to help them swim. Just like /4humans, frogs know that kicking with your legs, and using your arms helps you swim."/>
-
</list1>
-
<list2></list2>
-
<list2>
-
<project text="human being newly born" ID="B01_01"/>
-
<project text="the action of reaching a place" ID="U01_01"/>
-
<project text="an animal that can live on land and in water" ID="I01_01"/>
-
</list2>
-
</data>
-
As soon as I run the script, it will only show a text field showing "list1"
Thanks in advance.
| 
August 19th, 2008, 03:19 PM
| | Moderator | | Join Date: Mar 2006
Posts: 889
| |
I'm not entirely sure but it looks like these line:
newnavData = navData.children();
txtFldPassage.text = newnavData[slideNum].name().toString();
newnavData is probably on the List node now. Move 1 level further to the project nodes.
nextnavData = newnavData[0];
Also, don't just get the name of the node, get the value.
txtFldPassage.text = nextnavData[slideNum].value().toString();
(Note, you'll have to add your own error checking; eg index out of bounds errors.)
| 
August 21st, 2008, 09:11 AM
| | Newbie | | Join Date: Aug 2008
Posts: 4
| | |  |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|