Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old August 15th, 2008, 08:01 AM
Newbie
 
Join Date: Aug 2008
Posts: 4
Default 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:
Expand|Select|Wrap|Line Numbers
  1. var txtFldPassage:TextField = new TextField();
  2. txtFldPassage.multiline = txtFldPassage.wordWrap = true;
  3. txtFldPassage.type = TextFieldType.INPUT;
  4. txtFldPassage.border = txtFldPassage.background = true;
  5.  
  6. var slideNum:Number = 0; 
  7. var navData:XML;
  8. var newnavData:XMLList;
  9. var loader:URLLoader = new URLLoader();
  10. loader.addEventListener(Event.COMPLETE, onComplete, false, 0, true);
  11. loader.addEventListener(IOErrorEvent.IO_ERROR, onIOError, false, 0,
  12. true);
  13. loader.load(new URLRequest("data.xml"));
  14. function onComplete(evt:Event):void {
  15. try {
  16. navData = new XML(evt.target.data);
  17. newnavData = navData.children();
  18. trace(newnavData);
  19. txtFldPassage.text = newnavData[slideNum].name().toString();
  20. loader.removeEventListener(Event.COMPLETE, onComplete);
  21. loader.removeEventListener(IOErrorEvent.IO_ERROR,
  22. onIOError);
  23. } catch (err:Error) {
  24. trace("Could not parse loaded content as XML:\n" + err.
  25. message);
  26. }
  27. }
  28. function onIOError(evt:IOErrorEvent):void {
  29. trace("An error occurred when attempting to load the XML.\n" +
  30. evt.text);
  31. }
  32.  
  33.  
  34. addChild(txtFldPassage);
  35.  
and here's the XML file, data.xml

Expand|Select|Wrap|Line Numbers
  1. <?xml version='1.0' encoding='UTF-8'?>
  2. <data>
  3.   <list1>
  4.     <project text="When the Tail is Gone" mp3="a1.mp3"/>
  5.     <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."/>
  6.     <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."/>
  7.     <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."/>
  8.     <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."/>
  9.     <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."/>
  10.   </list1>
  11.   <list2></list2>
  12.   <list2>
  13.     <project text="human being newly born" ID="B01_01"/>
  14.     <project text="the action of reaching a place" ID="U01_01"/>
  15.     <project text="an animal that can live on land and in water" ID="I01_01"/>
  16.   </list2>
  17. </data>
  18.  
As soon as I run the script, it will only show a text field showing "list1"

Thanks in advance.
Reply
  #2  
Old August 19th, 2008, 03:19 PM
Moderator
 
Join Date: Mar 2006
Posts: 889
Default

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.)
Reply
  #3  
Old August 21st, 2008, 09:11 AM
Newbie
 
Join Date: Aug 2008
Posts: 4
Default

Here's the full ActionScipt 3, make sure to add all the components in the library

Expand|Select|Wrap|Line Numbers
  1. import fl.controls.ComboBox;
  2. import fl.controls.TextArea;
  3. import fl.containers.UILoader;
  4.  
  5. //Component Style
  6. import fl.managers.StyleManager;
  7.  
  8. var xmlLoader:URLLoader = new URLLoader();
  9. var dataURL:URLRequest = new URLRequest("data.xml");
  10. xmlLoader.addEventListener(Event.COMPLETE,xmlLoaded);
  11. xmlLoader.load(dataURL);
  12.  
  13. var tFormatHead:TextFormat = new TextFormat();
  14. tFormatHead.bold = true;
  15. tFormatHead.size = 12;
  16. tFormatHead.font = "Arial";
  17.  
  18.  
  19. var albumFormat:TextFormat = new TextFormat();
  20. albumFormat.font = "Arial";
  21. albumFormat.size = 12;
  22. albumFormat.bold = true;
  23.  
  24. StyleManager.setStyle("textFormat", albumFormat);
  25.  
  26. var dataXML:XML = new XML();
  27. dataXML.ignoreWhitespace = true;
  28.  
  29. // Choose Style Drop-Down
  30. var cbStyle:ComboBox = new ComboBox; // Combobox
  31. cbStyle.x = 755;
  32. cbStyle.y = 260;
  33. cbStyle.width = 100;
  34. addChild(cbStyle);
  35.  
  36. // Choose Style
  37. var tfStyleTxt:TextField = new TextField();
  38. tfStyleTxt.x = 750;
  39. tfStyleTxt.y = 240;
  40. tfStyleTxt.autoSize = TextFieldAutoSize.LEFT;
  41. tfStyleTxt.text = "Choose Style";
  42. addChild(tfStyleTxt);
  43. tfStyleTxt.setTextFormat(tFormatHead);
  44.  
  45. var txtFldPassage:TextArea = new TextArea(); // Text Area Description
  46. txtFldPassage.x = 30;
  47. txtFldPassage.y = 120;
  48. txtFldPassage.width = 700;
  49. txtFldPassage.height = 250;
  50. addChild(txtFldPassage);
  51.  
  52.  
  53. var txtFldQuestions:TextArea = new TextArea(); // Text Area City
  54. txtFldQuestions.x = 30;
  55. txtFldQuestions.y = 440;
  56. txtFldQuestions.width = 600;
  57. txtFldQuestions.height = 200;
  58. addChild(txtFldQuestions);
  59.  
  60. var txtFldFeedback:TextArea = new TextArea(); // Text Area feedback
  61. txtFldFeedback.x = 650;
  62. txtFldFeedback.y = 440;
  63. txtFldFeedback.width = 300;
  64. txtFldFeedback.height = 200;
  65. addChild(txtFldFeedback);
  66.  
  67.  
  68. function xmlLoaded(evt:Event):void
  69. {
  70.     dataXML = XML(xmlLoader.data);
  71.  
  72.     for (var tripName:String in dataXML.list)
  73.     {    
  74.         cbStyle.addItem({label:dataXML.list[tripName].@style});
  75.     }
  76.     txtFldPassage.text = dataXML.list[0].passage;
  77.     txtFldQuestions.text = dataXML.list[0].question;
  78.     txtFldFeedback.text = dataXML.list[0].feedback;
  79.  
  80. }
  81.  
  82. function selectStyle(evt:Event):void
  83. {
  84.     trace ("selected" + evt.target.selectedIndex);
  85.     txtFldPassage.text = dataXML.list[evt.target.selectedIndex].passage;
  86.     txtFldQuestions.text = dataXML.list[evt.target.selectedIndex].question;
  87.     txtFldFeedback.text = dataXML.list[evt.target.selectedIndex].feedback;
  88.  
  89.  
  90. }
  91. cbStyle.addEventListener(Event.CHANGE, selectStyle);
  92.  
  93.  
  94.  
  95.  
Here's the XML

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="iso-8859-1"?>
  2. <data>
  3.   <list style="Bold">
  4.     <question>the thing that will turn into a chicken after hatching</question>
  5.     <feedback>I think you got the correct answer. Click the word now to confirm if this is correct</feedback>
  6.     <passage>What is a /1baby frog called? Where does it come from? How does a baby frog grow into an adult frog?  This is the story of the life cycle of the frog.</passage>
  7.   </list>
  8.   <list style="Underline">
  9.     <question>a living creature that is not human</question>
  10.     <feedback>I think you got the correct answer. Click the word now to confirm if this is correct</feedback>
  11.     <passage>What is a baby frog called? Where does it /2come from? How does a baby frog grow into an adult frog?  This is the story of the life cycle of the frog.</passage>
  12.   </list>
  13.   <list style="Bracket ">
  14.     <question>small</question>
  15.     <feedback>I think you got the correct answer. Click the word now to confirm if this is correct</feedback>
  16.     <passage>What is a baby frog called? Where does it come from? How does a baby /3frog grow into an adult frog?  This is the story of the life cycle of the frog.</passage>
  17.   </list>
  18.   <list style="Font Color ">
  19.     <question>the seeds of frogs</question>
  20.     <feedback>I think you got the correct answer. Click the word now to confirm if this is correct</feedback>
  21.     <passage>What is a baby frog called? Where does it come from? How does a baby frog grow into an /4adult frog?  This is the story of the life cycle of the frog.</passage>
  22.   </list>
  23.   <list style="All Style ">
  24.     <question>an animal that can live on land and in water</question>
  25.     <feedback>I think you got the correct answer. Click the word now to confirm if this is correct</feedback>
  26.     <passage>What is a /1baby frog called? Where does it /2come from? How does a baby /3frog grow into an /4adult frog?  This is the story of the life cycle of the frog.</passage>
  27.   </list>
  28. </data>
  29.  
  30.  
Reply
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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.
Post your question now . . .
It's fast and it's free

Popular Articles