Solved

Flash + Reading XML File

Posted on 2004-08-25
2
246 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.
Suggested Courses

630 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