flash data integration:
HI, I am trying to learn xml in flash myself and am finding it v.hard! All i am trying to do is create a little 'story' with 3 pages.I have content loading into flash but I am unable to get the next 'sibling' into an array e.g. the title. I can only get page1's title in the array but I can never get to page 2. Here is my xml: <maincontent> <page1> <title>Who is he</title> <story>Duh a monkey that lives in the jungle, thats why they call him the JUNGLE MONKEY</story> </page1> <page2> <title>What does he do?</title> <story>Swings around from tree to tree singing do wa di di .. di di dum di di do...</story> </page2> <page3> <title>What is he like?</title> <story>Well you know - just a monkey doing his monkey business!</story> </page3> </maincontent> and here is my actionscript: //load in the xml file tut = new XML(); tut.ignoreWhite = true; tut.onLoad = function(success) { if (success) { processstory(tut); } }; tut.load("tut.xml"); function processstory(xmlDoc_xml) { titlearray = new Array(); // xmlDoc_xml is now a reference to the XML // object where our information is stored for (var n = 0; n<xmlDoc_xml.firstChild.childNodes.length; n++) { trace(n); if (xmlDoc_xml.firstChild.firstChild.childNodes[n].nodeName == "title") { trace('The title is'+' '+xmlDoc_xml.firstChild.firstChild.childNodes[n].firstChild.nodeValue); titlearray.push(xmlDoc_xml.firstChild.firstChild.childNodes[n].firstChild.nod eValue); } trace(titlearray); } } This only traces the first title, how can i get to the second and the third so i can then display them in my text box???? Any help/guidance or pointing to the right place is thankfully received! Thanks Bex
Howdy XML is great if you understand all the concepts of node navigation, but this takes too long and is not very interesting Macromedia introduced the XpathAPI to allow us mere mortals a faster learning curve. So here is what I have done with your code, using the XpathAPI The 3 key lines to look at are Line 1 import: mx.xpath.XPathAPI, this imports the Xpath Class and allows it to be used. Line 10 var thePath_str:String = "maincontent/page/title"; this tells Xpath Class what to look for Line 11 var title_array:Array = XPathAPI.selectNodeList(this.firstChild, thePath_str); this creates an Array of titles obtained from the firstChild node of this, which in our case is maincnontent/page/title or thePath_str which is the same. I also altered your XML slightly, so that it conforms uniformaly. Hope it all helps import mx.xpath.XPathAPI; var storyxml:XML = new XML(); storyxml.ignoreWhite = true; storyxml.onLoad = function(success:Boolean) { trace("onload..."); if (success) { trace("success..."); // Retrieve all title nodes within maincontent/page/title. var thePath_str:String = "maincontent/page/title"; var title_array:Array = XPathAPI.selectNodeList(this.firstChild, thePath_str); for (var i:Number = 0; i < title_array.length; i++) { trace(title_array[i].firstChild.nodeValue);} } else { trace("error loading XML"); } }; storyxml.load("story.xml"); <maincontent> <page id="1"> <title>Who is he</title> <story>Duh a monkey that lives in the jungle, thats why they call him the JUNGLE MONKEY</story> </page> <page id="2"> <title>What does he do?</title> <story>Swings around from tree to tree singing do wa di di .. di di dum di di do...</story> </page> <page id="3"> <title>What is he like?</title> <story>Well you know - just a monkey doing his monkey business!</story> </page> </maincontent>
Don't see what you're looking for? Try a search.
|