Hello.
I’ve gone through tis excellent set of tutorials:
http://www.kirupa.com/developer/flashcs3/using_xml_as3_pg1.htm
and wanted to say thanks for sharing that it’s really helping me out.
Now to my problem. I have a MovieClip which has a couple of text boxes embedded inside it. I’m trying to load data from an XML file and populate the text fields with this data. That’s fine, all good. What I also need to do is create an instance of that MovieClip for each instance of a certain tag in the XML, and then load that instance of the MovieClip with the relevant information.
So, for this XML file (reduced to show only 2 CD entries):
<?xml version="1.0" encoding="ISO-8859-1"?>
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
<CD>
<TITLE>Pavarotti Gala Concert</TITLE>
<ARTIST>Luciano Pavarotti</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>DECCA</COMPANY>
<PRICE>9.90</PRICE>
<YEAR>1991</YEAR>
</CD>
</CATALOG>
I’m using the following code:
package {
import flash.display.MovieClip;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.*;
import flash.errors.*;
import flash.events.Event;
public class e4x extends MovieClip {
private var mainXML:XML;
private var loader:URLLoader = new URLLoader();
public function e4x() {
// constructor code
loader.addEventListener(Event.COMPLETE, onComplete);
loader.load(new URLRequest("e4x.xml"));
}
private function onComplete(e:Event) {
try {
mainXML = new XML(loader.data);
} catch(e:Error) {
trace("xml error!" + e.message);
return;
}
trace("xml loaded, start parsing :-)");
//trace(mainXML);
parseBooks(mainXML);
makeCircs(mainXML);
}
private function parseBooks(xmlIn:XML):void {
trace("XML Output");
trace("---------- ---------");
trace(xmlIn.CD);
}
private function makeCircs(xmlIn:XML):void {
var titleList:XMLList = xmlIn.CD;
for each (var i:XML in titleList) {
var circ:myCirc = new myCirc();
stage.addChild(circ);
circ.texter.text = xmlIn.CD.ARTIST;
circ.texte.text = xmlIn.CD.COUNTRY;
circ.x = Math.random() * stage.width;
circ.y = Math.random() * stage.height;
}
}
}
}
So a new instance of myCirc needs to happen on each <CD> from the XML, and it’s text fields need to contain the information from the XML.
It’s working, but I get instances which contain all the text from all the <CD> sections of the XML, each one has all the titles, countries etc.
Any help here would be wonderful, I’ve been away form code for a long time and whilst I’m really enjoying fiddling my way through, I’m now stumped