View Full Version : Help Loading XML into Loader
theRemix
11-14-2007, 07:05 PM
i have a Loader object which loads a swf into a sprite.
within this class that extends Sprite i have a method to load data from an XML file. i want this string to be loaded into a certain dynamic text field inside the .swf movieclip loaded by Loader.
how do i do this?
this is what i want to do:
private var _loader:Loader = new Loader();
private function init():void{
_loader.load(new URLRequest(cfg.modelDir + _modelName + cfg.modelExt));
this.addChild(_loader);
//load xmls
initXML("external_file_name.xml");
}
private function initXML(xml_str:String):void{
var request:URLRequest = new URLRequest(cfg.xmlDir + xml_str + cfg.xmlExt);
xml_loader.load(request);
xml_loader.addEventListener(Event.COMPLETE, loadXML);
}
private function loadXML(event:Event):void{
var xml_loader:URLLoader = event.target as URLLoader;
var externalXML:XML;
if (xml_loader != null)
{
externalXML = new XML(loader.data);
_loader.content.dynamic_txt = externalXML.content[0];
}else{
trace("XMLloader is not a URLLoader!");
}
}
this is pretty much out of the AS3 help in flash.
_loader is the Loader object.
_loader.content is supposed to be the loaded swf movieclip (according to AS3 docs)
_loader.content.dynamic_txt is supposed to be a dynamic text field in the .swf file on root movieclip.
the error i get is:
1119: Access of possibly undefined property dynamic_txt through a reference with static type flash.display:DisplayObject.
i have a feeling i need to set up some event handler for when the Loader.content is done loading (using a Loader.contentLoaderInfo event and listener), but i don't know how to do this. how can i make both events run? how do i nest all the xml stuff into an LoaderInfo.INIT event handler?
nyghtrunner
11-14-2007, 07:51 PM
I could be wrong, but I don't see where you are actually loading the SWF into a sprite...
You have a Loader instance you are adding to the stage, but you are never saying that it's anything but a loader...
Then you are trying to access the box via the loader content... Which is not something that you can really do, since a loader isn't a sprite or movieClip or anything....
externalXML = new XML(loader.data);
_loader.content.dynamic_txt = externalXML.content[0]; //problem... _loader.content may refer to a swf, but it isn't a swf...
Try something more like this:
var mySprite:Sprite = Sprite(_loader.content);
trace(mySprite.dynamic_txt) //should return something like [object TextBox] or something, not sure.
I haven't actually tried this with a sprite, so my syntax might be a little off, but... I've done it with movieClips as in this Example:
var mcl:Loader = new Loader(); //creates the loader
var tempClip:MovieClip = loadArray[i]; //this is for pathing purposes
var loadURL:String = myResult.swfPath.(@id==i).piece.(@id==changerNum). path; //part of XML to grab a specific SWF path
var loadURLReq:URLRequest = new URLRequest(loadURL);
mcl.load(loadURLReq); //should be obvious...
mcl.contentLoaderInfo.addEventListener(Event.INIT, initImage);
var loadHolderClip:MovieClip = new MovieClip(); //another empty movieClip, but should maybe be a sprite...
tempClip.addChild(loadHolderClip); //adds the instance to the stage
loadHolderArray[i] = loadHolderClip; //stores it in an array for easy access
var loadedClip:MovieClip; //defines the final resting place of the loaded SWF. Again, could be sprite
function initImage(event:Event):void {
loadedClip = MovieClip(mcl.content); //actually loads the swf into something I can then use and later refer back to for attribs and whatnot.
loadHolderClip.addChild(loadedClip); //should be obv...
//Carry on more functionality...
Does this answer your question? I can go into more detail if you need, or maybe actually set one up with a Sprite for you as a simple example.
nyghtrunner
11-14-2007, 08:04 PM
Also, you might want to take a look at this thread, where I ran into a similar problem, and solved it.
http://www.actionscript.org/forums/showthread.php3?t=152077
That gives a lot more detail about how I'm doing everything, and then solving the problem. The first part has a good bit of code that I don't think you need, since it's setting up my XML load and all, but still might be helpful. :)
theRemix
11-14-2007, 08:36 PM
i see, so you aren't just addChild()ing the loaded content as a child of that class, you are creating a holder clip for it...
but i don't see how you access that loaded clip's content, and more importantly when (via handlers).
obviously it would be something like loadedClip.dynamic_txt but that won't be available until initImage
so would you mind showing me (working) code that loads xml data into a (fictional) dynanic text instance (named: dynamic_txt perhaps?) on the loaded swf?
btw, i read that other thread, and i think it's a little too advanced for me :\
theRemix
11-14-2007, 09:01 PM
ok i tried this,
var mc:MovieClip = MovieClip(_loader.content);
trace(mc.txt_1);
but i get this error
From Output window:
TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::Loader@2a868b1 to flash.display.MovieClip.
when i try this though.
var mc:Sprite = Sprite(_loader.content);
trace(mc.txt_1);
i get this from CompilerErrors window
1119: Access of possibly undefined property txt_1 through a reference with static type flash.display:Sprite.
matbury
11-14-2007, 11:17 PM
i see, so you aren't just addChild()ing
I like that. 'addChild()ing' I mean. I'm gonna petition the Oxford English Dictionary to have that added to the English language.
I also think they should add a new meaning to the word 'flashing'.
theRemix
11-15-2007, 01:22 AM
ok somehow i got it.
i wrapped all of the xml loading and handling inside of the Loader INIT handler (duh, why didn't i think of this before), and i called LoaderInfo.content (and Loader.contentLoaderInfo.content) where i was calling Loader.content
this makes more sense... sort of... after reading the documentation up and down several times.
anyway, thanks nyghtrunner !
nyghtrunner
11-15-2007, 01:22 AM
Ok,
Sorry it took me so long to get back to you on this... I had to take care of some things after work... :rolleyes:
Anyways, I have a working example for you. Here's the VERY simple XML I wrote:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<data>
<swf>test/myTxt.swf</swf>
<desText>This is the text I want in my external SWF. It should be loaded in dynamically, and I would love it if it worked... =)</desText>
</data>
There are 2 nodes, 1 with the SWF path, and 1 with the Text to load into it. It lives in the project folder.
The SWF has nothing but a dynamic TXT field on the main timeline @ fram 1. I gave the box an instance name of "myText_txt", and set the line type to "multiline" (it defaulted to single, but might have just been how I created it...).
Anyways, I named that file MyTxt.fla, and compiled a SWF in a folder called "test" in my project folder. Changing and embedding fonts is a different lesson, so I just left it to the default of Times.
Next, in my project folder (not the "test" subfolder!!!), I created my main FLA. There's nothing but code here, in the first frame. One thing of note, is that I tried the final piece as a Sprite, but that didn't work. I'm interested in this, because it seems like it should work off the top of my head, but then I realized, a Sprite is basically a movieClip without a timeline. I used the timeline in the myTxt.fla to hold the text box, making me think that's why I couldn't use a Sprite as a "load" target... It makes me wonder if it would work if I created the box dynamically, but that is beyond the scope of this example.
Anyways, here's the code for the main FLA file. It lives in frame 1. I commented it some, but really just the traces. The code should be pretty self explanatory and straight forward. I'm not really doing anything fancy here! :)
import flash.display.*;
import flash.events.*;
import flash.filters.*;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.xml.*;
import flash.net.URLRequest;
import flash.text.*;
var myResult:XML; //declared here so we have access to it throughout. You could "toGlobal" it, however.
function loadXML():void {
var loader:URLLoader=new URLLoader();
loader.addEventListener(Event.COMPLETE,completeHan dler);
var myRequest:URLRequest=new URLRequest("example.xml");
try {
loader.load(myRequest);
}
catch(error:Error) {
trace("unable to load requested URL");
}
}
function completeHandler(event:Event):void {
var loader:URLLoader=URLLoader(event.target);
myResult=new XML(loader.data);
trace(myResult.swf); //traces the path to the swf itself
loadSwf();
}
function loadSwf():void {
var mcl:Loader = new Loader();
var tempClip:Sprite = new Sprite();
addChild(tempClip);
var loadURL:String = myResult.swf; //for loading the SWF
var loadURLReq:URLRequest = new URLRequest(loadURL);
mcl.load(loadURLReq);
mcl.contentLoaderInfo.addEventListener(Event.INIT, initImage);
var loadedClip:MovieClip;
function initImage(event:Event):void {
trace(mcl.content); //traces [object MainTimeline], meaning it has access to the SWF contents
loadedClip = MovieClip(mcl.content);
tempClip.addChild(loadedClip);
trace(loadedClip); //traces [object MainTimeline] *See above
trace(loadedClip.myText_txt); //traces [object TextField]
trace(myResult.desText); //traces the text in the XML file
loadedClip.myText_txt.text = myResult.desText; //binds the dynamic text to the loaded SWF text field
}
}
loadXML();
So that's it. I'm posting a zip file of my working example here for you to DL and look at if you want.
me
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.