External XML into ActionScript 3.0 Text Field

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • saijin
    New Member
    • Aug 2008
    • 4

    #1

    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:
    Code:
    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

    Code:
    <?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.
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    I'm not entirely sure but it looks like these line:
    newnavData = navData.childre n();
    txtFldPassage.t ext = newnavData[slideNum].name().toStrin g();

    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.t ext = nextnavData[slideNum].value().toStri ng();

    (Note, you'll have to add your own error checking; eg index out of bounds errors.)

    Comment

    • saijin
      New Member
      • Aug 2008
      • 4

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

      Code:
      import fl.controls.ComboBox;
      import fl.controls.TextArea;
      import fl.containers.UILoader;
      
      //Component Style
      import fl.managers.StyleManager;
      
      var xmlLoader:URLLoader = new URLLoader();
      var dataURL:URLRequest = new URLRequest("data.xml");
      xmlLoader.addEventListener(Event.COMPLETE,xmlLoaded);
      xmlLoader.load(dataURL);
      
      var tFormatHead:TextFormat = new TextFormat();
      tFormatHead.bold = true;
      tFormatHead.size = 12;
      tFormatHead.font = "Arial";
      
      
      var albumFormat:TextFormat = new TextFormat();
      albumFormat.font = "Arial";
      albumFormat.size = 12;
      albumFormat.bold = true;
      
      StyleManager.setStyle("textFormat", albumFormat);
      
      var dataXML:XML = new XML();
      dataXML.ignoreWhitespace = true;
      
      // Choose Style Drop-Down
      var cbStyle:ComboBox = new ComboBox; // Combobox
      cbStyle.x = 755;
      cbStyle.y = 260;
      cbStyle.width = 100;
      addChild(cbStyle);
      
      // Choose Style
      var tfStyleTxt:TextField = new TextField();
      tfStyleTxt.x = 750;
      tfStyleTxt.y = 240;
      tfStyleTxt.autoSize = TextFieldAutoSize.LEFT;
      tfStyleTxt.text = "Choose Style";
      addChild(tfStyleTxt);
      tfStyleTxt.setTextFormat(tFormatHead);
      
      var txtFldPassage:TextArea = new TextArea(); // Text Area Description
      txtFldPassage.x = 30;
      txtFldPassage.y = 120;
      txtFldPassage.width = 700;
      txtFldPassage.height = 250;
      addChild(txtFldPassage);
      
      
      var txtFldQuestions:TextArea = new TextArea(); // Text Area City
      txtFldQuestions.x = 30;
      txtFldQuestions.y = 440;
      txtFldQuestions.width = 600;
      txtFldQuestions.height = 200;
      addChild(txtFldQuestions);
      
      var txtFldFeedback:TextArea = new TextArea(); // Text Area feedback
      txtFldFeedback.x = 650;
      txtFldFeedback.y = 440;
      txtFldFeedback.width = 300;
      txtFldFeedback.height = 200;
      addChild(txtFldFeedback);
      
      
      function xmlLoaded(evt:Event):void
      {
      	dataXML = XML(xmlLoader.data);
      	
      	for (var tripName:String in dataXML.list)
      	{	
      		cbStyle.addItem({label:dataXML.list[tripName].@style});
      	}
      	txtFldPassage.text = dataXML.list[0].passage;
      	txtFldQuestions.text = dataXML.list[0].question;
      	txtFldFeedback.text = dataXML.list[0].feedback;
      
      }
      
      function selectStyle(evt:Event):void
      {
      	trace ("selected" + evt.target.selectedIndex);
      	txtFldPassage.text = dataXML.list[evt.target.selectedIndex].passage;
      	txtFldQuestions.text = dataXML.list[evt.target.selectedIndex].question;
      	txtFldFeedback.text = dataXML.list[evt.target.selectedIndex].feedback;
      	
      	
      }
      cbStyle.addEventListener(Event.CHANGE, selectStyle);
      Here's the XML

      Code:
      <?xml version="1.0" encoding="iso-8859-1"?>
      <data>
        <list style="Bold">
          <question>the thing that will turn into a chicken after hatching</question>
          <feedback>I think you got the correct answer. Click the word now to confirm if this is correct</feedback>
          <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>
        </list>
        <list style="Underline">
          <question>a living creature that is not human</question>
          <feedback>I think you got the correct answer. Click the word now to confirm if this is correct</feedback>
          <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>
        </list>
        <list style="Bracket ">
          <question>small</question>
          <feedback>I think you got the correct answer. Click the word now to confirm if this is correct</feedback>
          <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>
        </list>
        <list style="Font Color ">
          <question>the seeds of frogs</question>
          <feedback>I think you got the correct answer. Click the word now to confirm if this is correct</feedback>
          <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>
        </list>
        <list style="All Style ">
          <question>an animal that can live on land and in water</question>
          <feedback>I think you got the correct answer. Click the word now to confirm if this is correct</feedback>
          <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>
        </list>
      </data>

      Comment

      Working...