Jump to content
Search Community

Custom XML parsing in onRawLoad handler

Philip Bulley test
Moderator Tag

Recommended Posts

Hi Jack,

 

I have a suggestion (forgive me if there's a way to do this already). In the onRawLoad handler of an XMLLoader, it'd be great if it were possible to:


  • [*:1hhx0adf] Get the loaded XML content
    [*:1hhx0adf] Convert it to a string ( XML.toXMLString() )
    [*:1hhx0adf] Parse that string (for example using String.replace() with regex)
    [*:1hhx0adf] Finally convert the string back to XML ( XML( myString ) ) and inject back into the event.target (XMLLoader)

The only roadblock here is that XMLLoader.content seems to be read-only, and I imagine for good reason. So maybe there could be a method on XMLLoader called injectXML(), initXML() or even replaceXML() which would allow this.

 

The use case is that I could place tokens absolutely anywhere in my XML (not just in the url attributes) and have LoaderMax replace them before XMLLoader.parseLoaders() is executed.

Link to comment
Share on other sites

  • 1 month later...

Hey Jack,

 

That's great if you want to edit the same XML object. But in my case, I want to convert the XML object to a String, perform a mass of regular expressions, cast the String back to XML and assign back to XMLLoader.content (I definitely want to assign back, rather than store elsewhere, so I can retain all the awesomeness of XMLLoader later).

 

Converting to a String is much faster than using E4X to traverse the XML object. My process takes:

E4X: 3198ms (during which the UI locks up)

String: 29ms

 

So I added the following to LoaderCore, which works great for me, but it may we worth adding this feature to the distribution?

 

public function set content( value:* ):void {
		_content = value;
	}

Link to comment
Share on other sites

Thanks for the suggestion, but I don't think it would be appropriate to make the "content" property writable. Loaders are for loading stuff and it could be dangerous to allow folks to set the content to whatever they please, thus not reflecting what was actually loaded from that URL. However, I'd offer two other potential solutions if you want to associate your content with the loader:

 

1) Just gut the contents of the root node of the XML and replace them with your stuff after you make your edits.

 

2) Store a reference in the "vars" object, like myLoader.vars.xml = yourXML.

Link to comment
Share on other sites

  • 2 weeks later...

Thanks for your suggestions... this first is a nice idea! :)

 

For reference, this is roughly what I ended up with:

 

var xmlStr:String = xml.children().toXMLString();
// start looping through tokens
var rule:RegExp = new RegExp( '%myToken%', 'g' );
xmlStr = xmlStr.replace( rule, 'myTokenReplacementString' );
// end loop
xml.setChildren( XMLList( xmlStr ) );

 

thanks!

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...