Solved

Flash + Reading XML File

Posted on 2004-08-25
2
239 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
The last time I worked with Flash and Socket connections was in AS1. A recent project required flash connecting to a Socket, and sending receiving information - we figured it would be easy enough - we all know about the socket policy documents and c…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The goal of the tutorial is to teach the user how to use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now