Solved

Flash + Reading XML File

Posted on 2004-08-25
2
243 Views
Last Modified: 2013-11-19
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
0
Comment
Question by:dkilby
2 Comments
 
LVL 39

Accepted Solution

by:
blue-genie earned 250 total points
ID: 11900211
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
 
LVL 4

Assisted Solution

by:mrdilliard
mrdilliard earned 250 total points
ID: 11910139
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question