dcsimg
A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Parsing XML in Flash

  1. #1
    Junior Member
    Join Date
    Apr 2003
    Location
    Knoxville, TN
    Posts
    24

    Parsing XML in Flash

    i just started messing around with parsing xml files today in flash. it's not really as hard as i thought, but i was wondering if anyone has any easier/better techniques of going about parsing an xml file. i'm creating a game and the process i'm having to go through to get the information keeps getting longer and longer... i'm wondering what would be easier. here is the process i go through, and this is just an example of one topic, and 5 questions with 3 possible answers for each... the xml file actually gets alot more complicated:

    Code:
    function loadMe() {
    //show episode 1's information
    	trace("-------------------------");
    	trace("Episode 1 = " + level2Child.attributes["name"]);
    		trace("Topic 1 = " + level3Child.attributes["name"]);
    			trace("Question 1 = " + level4Child.attributes["name"]);
    				trace("Answer 1 = " + level6Child.nodeValue + ", Correct = " + level5Child.attributes["correct"]);
    				trace("Answer 2 = " + level5Child.nextSibling.firstChild.nodeValue + ", Correct = " + level5Child.nextSibling.attributes["correct"]);
    				trace("Answer 3 = " + level5Child.nextSibling.nextSibling.firstChild.nodeValue + ", Correct = " + level5Child.nextSibling.nextSibling.attributes["correct"]);
    			trace("Question 2 = " + level4Child.nextSibling.attributes["name"]);
    				trace("Answer 1 = " + level6Child.nodeValue + ", Correct = " + level5Child.attributes["correct"]);
    				trace("Answer 2 = " + level5Child.nextSibling.firstChild.nodeValue + ", Correct = " + level5Child.nextSibling.attributes["correct"]);
    				trace("Answer 3 = " + level5Child.nextSibling.nextSibling.firstChild.nodeValue + ", Correct = " + level5Child.nextSibling.nextSibling.attributes["correct"]);
    			trace("Question 3 = " + level4Child.nextSibling.nextSibling.attributes["name"]);
    				trace("Answer 1 = " + level6Child.nodeValue + ", Correct = " + level5Child.attributes["correct"]);
    				trace("Answer 2 = " + level5Child.nextSibling.firstChild.nodeValue + ", Correct = " + level5Child.nextSibling.attributes["correct"]);
    				trace("Answer 3 = " + level5Child.nextSibling.nextSibling.firstChild.nodeValue + ", Correct = " + level5Child.nextSibling.nextSibling.attributes["correct"]);
    			trace("Question 4 = " + level4Child.nextSibling.nextSibling.nextSibling.attributes["name"]);
    				trace("Answer 1 = " + level6Child.nodeValue + ", Correct = " + level5Child.attributes["correct"]);
    				trace("Answer 2 = " + level5Child.nextSibling.firstChild.nodeValue + ", Correct = " + level5Child.nextSibling.attributes["correct"]);
    				trace("Answer 3 = " + level5Child.nextSibling.nextSibling.firstChild.nodeValue + ", Correct = " + level5Child.nextSibling.nextSibling.attributes["correct"]);
    			trace("Question 5 = " + level4Child.nextSibling.nextSibling.nextSibling.nextSibling.attributes["name"]);
    				trace("Answer 1 = " + level6Child.nodeValue + ", Correct = " + level5Child.attributes["correct"]);
    				trace("Answer 2 = " + level5Child.nextSibling.firstChild.nodeValue + ", Correct = " + level5Child.nextSibling.attributes["correct"]);
    				trace("Answer 3 = " + level5Child.nextSibling.nextSibling.firstChild.nodeValue + ", Correct = " + level5Child.nextSibling.nextSibling.attributes["correct"]);
    }
    
    myXML = new XML();
    myXML.ignoreWhite = true;
    myXML.onLoad = loadMe;
    myXML.load("myfile.xml");
    as you can see, it adds up quickly, and actually... the Answers are the same because i'm stuck on how to get at the other answers to the questions that are embedded in these other questions. i'll keep messing with it, but i thought i'd post this to help anyone out and to also see if there is a better way to get this info. thanks!

  2. #2
    Japanese l337 TRJNET's Avatar
    Join Date
    Mar 2001
    Location
    Toronto, Canada
    Posts
    399
    Hey bud, if you are really into Flash & XML like I think you are, check this site out if you are really into open-sourcing. Problem is with me, Im still trying to ge the whole concept of using XML within Flash: http://www.levitated.net
    consultant / contractor / designer

  3. #3
    Junior Member
    Join Date
    Apr 2003
    Location
    Knoxville, TN
    Posts
    24
    i love xml... i've been reading up on it for a while and it's actually a really easy format to learn. there are basically no rules in how you build the file, except to follow the format. but what i'm wanting to use XML for is a game.

    for example... in this game, there is different types of 'episodes'. like... Technology, Transportation, etc...

    then there are topics under Technology (for example) like Computers, PDA's, etc...

    then there are questions under each of those topics, and answers, etc...

    so i'm wanting to dynamically pull in that info and place it accordingly. anyway, i'll check out your link here in a sec. thanks!

  4. #4
    Senior Member
    Join Date
    Dec 2002
    Location
    Kansas City, MO. USA
    Posts
    119
    I do most of my Flash with XML and really like putting stuff into Associative (sp?) arrays. I'll end up with a load function like:

    function getXML(success) {
    if (success==false) { errorCheck("-");
    } else {
    if (data_xml.status == 0) {
    arySection = new Array();
    counter = 0;
    rootNode = data_xml.firstChild;
    childNodesArray = rootNode.childNodes;
    for (a = 0; a < childNodesArray.length; a++) {
    if (childNodesArray[a].nodeName == "edition") {
    if (childNodesArray[a].attributes.pgID == pgID) {
    grandChildNodesArray = childNodesArray[a].childNodes;
    for (b = 0; b < grandChildNodesArray.length; b++) {
    thisData = new Object();
    thisData.name = grandChildNodesArray[b].attributes.name;
    thisData.image = grandChildNodesArray[b].attributes.image;
    thisData.title = grandChildNodesArray[b].attributes.title;
    thisData.paragraph = grandChildNodesArray[b].attributes.paragraph;
    thisData.url = grandChildNodesArray[b].attributes.url;
    thisData.color = grandChildNodesArray[b].attributes.color;
    //errorCheck(thisData);
    arySection.push(thisData);
    }
    }
    }
    }
    //report();
    }
    }
    }

    So then I would get the data via

    arySection[x].name
    arySection[x].image
    etc.

    The project I pulled this from currently has 257 images but by just editing the XML I can go down to 0 and as many as I want.

    Does this help any?
    Michael [the idiot] Barsotti

  5. #5
    Junior Member
    Join Date
    Apr 2003
    Location
    Knoxville, TN
    Posts
    24
    i like the idea of putting the xml data into arrays. i'll definitly mess with that. the way you've set it up is a great idea actually. i'll see if i can get it going with the layout i have now. i appreciate that!

  6. #6
    Junior Member
    Join Date
    Apr 2003
    Location
    Knoxville, TN
    Posts
    24
    wow! check out this cool little script i wrote to go through my elements in the xml file...it's real simple! this is pretty much setup to work only with my xml file, however...it can be adjusted to work on others, i'm sure. now i just need to take out the values i get and put them into arrays so i can access them easier instead of simply 'tracing()' them to see the values.

    thanks for the direction, guys!

    Code:
    function getXML() {
    	//show how many episodes there are
    	trace("(There are " + this.firstChild.childNodes.length + " episodes available.)");
    	trace(" ");
    	//get the episodes
    	for (i = 0; i < this.firstChild.childNodes.length; i++) {
    		trace("Episode " + (i + 1) + ": " + this.firstChild.childNodes[i].attributes["name"]);
    		//get the topics
    		for (j = 0; j < this.firstChild.firstChild.childNodes.length; j++) {
    			trace("  Topic " + (j + 1) + ": " + this.firstChild.childNodes[i].childNodes[j].attributes["name"]);
    			//get the questions
    			for (k = 0; k < this.firstChild.firstChild.firstChild.childNodes.length; k++) {
    				trace("    Question " + (k + 1) + ": " + this.firstChild.childNodes[i].childNodes[j].childNodes[k].attributes["name"]);
    				//get the answers
    				for (l = 0; l < this.firstChild.firstChild.firstChild.firstChild.childNodes.length; l++) {
    					trace("      Answer " + (l + 1) + ": " + this.firstChild.childNodes[i].childNodes[j].childNodes[k].childNodes[l].firstChild.nodeValue +" Correct: " + this.firstChild.childNodes[i].childNodes[j].childNodes[k].childNodes[l].attributes["correct"]);
    				}
    			}
    		}
    	}
    }

  7. #7
    Mod cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    Here are a couple of xml tutorials, how to make a search engine, a menu and under components a search engine and some xml based slideshows/menu. Some of it is also on flashkit site but here it is more complete.
    http://can_info_guide.tripod.com/
    - The right of the People to create Flash movies shall not be infringed. -
    | www.Flashscript.biz | Flashscript Biz Classes/Components |

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center