Jump to content
Search Community

Ability to modify embedded XMLLoader URLs before they load

ggritmon test
Moderator Tag

Recommended Posts

I have an app where relative loading is not an option. Instead, I use recursivePrependURLs with the absolute location of the files specified in the XML loaded via XMLLoader. Unfortunately, some file locations include the '../', which when combined with a recursivePrependURL is invalid




asset URL = "../images/bar.jpg"


expected result = "http://www.foo.com/images/bar.jpg"

actual result = "http://www.foo.com/widgets/../images/bar.jpg"


I've written a validator on my end which scans for ../ in the middle of a URL and removes the preceding folder, but am not sure where I would have an opportunity to modify the embedded URL from the XML before LoaderMax begins loading it.


Could you please advise on when I'd have an opportunity to modify the URL at runtime before loading, or perhaps add support for this case in a future version of the library?

Link to comment
Share on other sites

  • 7 months later...


Essentially what I want to do is replace any instances of @lang@ in my XML to the user's selected language.



<ImageLoader url="@lang@/imagefile.jpg">

should become:

<ImageLoader url="en/imagefile.jpg">


I've got the following listener, but it throws an error when I try to write the XML content back to the XMLLoader.

private function _onRawLoad(e:LoaderEvent):void 
  var raw:String = (e.target.content as XML).toXMLString();
  raw = StringUtils.format(raw, { lang:"ENGLISH" } );
  e.target.content = new XML(raw);


Please advise.

Link to comment
Share on other sites

Actually, it's even simpler than that. You see, XML in Flash is treated like a regular Object, so you can make changes to it and pass it around as much as you want. If you edit the XML object, its properties, etc., you don't need to then pass it back to the XMLLoader and tell it to replace something internally. For example, think about a regular object:


var candy:Object = {flavor:"chocolate"};
var reference:Object = candy;
candy.flavor = "vanilla";
trace(reference.flavor); //"vanilla"


Same goes for XML. So you can get the XMLLoader's "content" (the XML) in the onRawLoad and make edits to it as much as you want and then when it gets parsed (right after onRawLoad gets called and your code runs), the new data will be in there.


The timing is the key - Flash is single-threaded, so everything runs in sequence. The XML loads, then onRawLoad gets called, your code inside the onRawLoad runs, then XMLLoader parses the XML internally.


Make sense?


But remember, you should edit the raw XML object itself - don't convert it to a String and back again because that loses the reference. If you need to convert things to a string, just make sure that when you make it back into XML, you insert it into the original root node of the XML object you got from XMLLoader and replace its contents.

Link to comment
Share on other sites

Thanks. I wasn't aware that modifying an XMLList would update the origin XML. That helped a lot! Final method looks like this:


 private function _onRawLoad(e:LoaderEvent):void
  var url:String;
  var xmlList:XMLList = e.target.content..@url;
  for each (var node:XML in xmlList)
   url = node.parent().@url;
   url = StringUtils.format(url, { lang:profile.language, quality:profile.quality } );
   node.parent().@url = url;

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...