Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Flash + Reading XML File

Posted on 2004-08-25
2
Medium Priority
?
248 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 1000 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 1000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
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 viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

719 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