Flash + Reading XML File

I am trying to read and xml file from Flash.  I want to list different Categories, then inside there are questions, and answers, here is an example of the xml file

<lists xmlns="http://goadygames.com/wordlists.xsd">
    <list category="Soccer">
        <question category="Married To Posh Spice">
      <word>David Beckham</word></question>
        <question category="Won Most World Cups">
      <word>Brazil</word></question>
        <question category="Won Euro 2004">
      <word>Greece</word></question>
        <question category="Won 1966 World Cup">
      <word>Englan</word></question>
</lists>

I am not sure if i have a proper XML file to start with,  in FLASH i want to create 3 arrays, on that will list the category and then the questions, then the answers.

      var temp:Array = new Array();
      var temp:XMLNode = doc.firstChild.childNodes;
      var scrollingList:Array = new Array();
      for (var i = 0; i<temp.length; ++i) {
            var tempList:Array = temp[i].childNodes;
            var category:String = temp[i].attributes.category;
            var wordArray:Array = new Array();
            for (var j = 0; j<tempList.length; ++j) {
                  var word:String = tempList[j].firstChild.nodeValue;
                  wordArray.push(word);
            }
            words.lists.push({questionList:wordArray, category:category});
            scrollingList.push({label:category, data:i});
      }
      scrollList.dataProvider = scrollingList;

this is what i have so far, the catergory list comes up okay but i can't seem to read the questions, and i am not sure how to get the answers.

Thanks
dkilbyAsked:
Who is Participating?
 
blue-genieConnect With a Mentor Commented:
Hi, in terms of reading the xml you need to use something like this: (this is from a file where i attach movie clips to create btns in a menu)

menuXml = new XML();
menuXml.ignoreWhite = true;
menuXml.onLoad = function(success) {
      if (success) {
            menuItem = this.firstChild.childNodes;
                  for (var i = 2; i<menuItem.length; i++) {
                  counter=i-2;
                  item = _root.attachMovie("itemClip", "itemClip"+i, i);
                  item._x = 150;
                  item._y = 50*i;
                  item.itemLabel.text = menuItem[i].attributes.text;
                  item.myUrl = menuItem[i].attributes.url;
                  trace(item.myURL add " rul");
                                    item.onRelease = function() {
                        this.gotoAndStop(1);
                        getURL(this.myURL, "_blank");
                  };
            }
      }
};
menuXml.load("myMenu.xml");

in terms of the xml - (I suck at xml) - the format i used in this example is:
<?xml version="1.0" encoding= "UTF-8" ?>
<menu>
<button text="Google"  url="http://www.google.com" target="_parent"></button>
<button text="Yahoo"  url="http://www.yahoo.com" target="_parent"></button>
<button text="Hotmail"  url="http://www.hotmail.com" target="_parent"></button>
<button text="CNN"  url="http://www.cnn.com" target="_parent"></button>
<button text="Microsoft"  url="http://www.microsoft.com" target="_parent"></button>
</menu>

(using attributes) -

don't know if this helps - or if its just a waste of space.

blu.

0
 
mrdilliardConnect With a Mentor Commented:
I think you should probably do your XML file a little bit differently. Make sure you keep the attributes the same (i.e. "name", "question" and "answer")

==============
<questions>
      <category name="Soccer">
        <item question="Married To Posh Spice" answer="David Beckham" />
        <item question="Won Most World Cups" answer="Brazil" />
        <item question="Won Euro 2004" answer="Greece" />
        <item question="Won 1966 World Cup" />
      </category>
      <category name="Web Development">
        <item question="What does XML stand for" answer="Extensible Markup Language" />
        <item question="What is a great animation program" answer="Flash MX 2004" />
      </category>
</questions>
==========

Accordingly, here is the flash to read it in. Just replace "questions.xml" with the name of your xml file.

============
var arrayCategories:Array = new Array();
var arrayQuestions:Array = new Array();
var arrayAnswers:Array = new Array();
var xmlList:XML = new XML();
//don't keep track of white space in XML file
xmlList.ignoreWhite = true;
xmlList.onLoad = function(success) {
      var xLevel = this.firstChild.firstChild;
      if (success) {
            //loop through the top necessary level, the categories
            while (xLevel) {
                  //add the category name
                  arrayCategories.push(xLevel.attributes["name"]);
                  var yLevel = xLevel.firstChild;
                  //loop through the questions within a category
                  while (yLevel) {
                        //attach the name of the category within the stored value for filtering
                        arrayQuestions.push(xLevel.attributes["name"]+":"+yLevel.attributes["question"]);
                        arrayAnswers.push(xLevel.attributes["name"]+":"+yLevel.attributes["answer"]);
                        //use these instead for not putting the category along with the question or answer
                        //arrayQuestions.push(yLevel.attributes["question"]);
                        //arrayAnswers.push(yLevel.attributes["answer"]);
                        yLevel = yLevel.nextSibling;
                  }
                  xLevel = xLevel.nextSibling;
            }
      }
};
xmlList.load("questions.xml");
==============

I hope this works for you. One problem you'll also need to overcome will be filtering the questions based on the current category.

I've found that using while loops rather than for loops works a little better for me. Obviously, it's just personal preference. I also use firstChild rather than childNodes.

You may find these sites helpful
http://kirupa.com/developer/actionscript/xmlfromflash.htm
http://kirupa.com/web/xml_php_parse_beginner.htm

Not necessarily best practices, but I think they can get you going. By the way, you don't need to define an xsd for your xml file, since you're not really displaying it, but only reading it into flash.

Have fun!
0
All Courses

From novice to tech pro — start learning today.