E4X Doesn’t Suck

I’ve spoken on past occasions about how un-excited I am about E4X (ECMAScript for XML) with regards to ActionScript 3. Macromedia (now Adobe) made a strong push for XML for 3 player versions. Flash Player 5 had both a built-in XML parser as well as the XMLSocket connection. Flash Player 6 greatly optimized the XML parsing routines and Flash Player 7 did it again.

Problem was, for application developers dealing with nTier systems (fancy way of saying connecting to various backends), XML wasn’t the most succinct way of communicating, and those messages sometimes were a non-portable, made up language. By then, I was delving into the worlds of WebServices and Remoting. While SOAP implementations utilize XML in the underlying guts of how it works, you as the developer certainly feel like you’re writing regular code talking to code on some other server.

So, in building a prototype app using Flex 2, Cairngorm 2, and Ruby on Rails, I’m dealing with copious amounts of XML. I feel like I’m in middle-school again. It frightens me to hear myself say that as that same arrogant, holier-than-though attitude of non-big-company technologies is what I used to loathe when talking to Enterprise programmers. I can remember not just me, but countless others accomplishing a lot with just XML strings parsed by hand with very effective outcomes.

Either way, the thing that is different now is that I have E4X at my disposal. I’m the only one on the planet who feels that the old syntax is ok to use instead and isn’t overly verbose. I guess growing up with firstChild.childNodes makes you numb to the pig-latin you speak in code to get at your data. E4X, unintentionally, I perceived as more verbose because I am trying to learn as well as all the ActionScript 3 packages, mx packages, and countless other new API’s & methodologies at the same time. Spending 40 minutes on getting a new for each loop to work, referring to the docs twice, is all the temptation I need to say, “Fuggit, the old way works just fine, I know it well, and it’s faster at runtime than E4X anyway… why bother with this bs when the syntax isn’t that much cleaner?”

Many would disagree. I debated with the top Flash minds twice in the past, all with next to no support. Many fawn over it. While I was visiting my boss in Des Moines, Iowa, he took it upon himself one evening to show me some refactored XML to E4X code. He was overjoyed… I didn’t care. Granted I was happy he was happy. He went from 5 verbose lines to 3 cleaner ones.

“Ok… you’re still parsing… parsing blows.”

I don’t have a choice with Ruby on Rails. Maybe I do, and don’t know it. There is so much stuff I don’t know about ROR, but I’m going with what I got, and what I got is a consistent and readable messaging schema between Flex and Rails. Rails sends easily understood XML messages, and the ones I send to it, it can read just fine. However, after writing my Delegate to speak the CRUD methods to my Rails controller methods, I really wasn’t looking forward to writing a gigantic Factory class to convert Rails XML messages to Array’s of Value Objects, and vice-versa.

I’m still not, but found a really easy way to implement it. And thus, I get to why E4X doesn’t suck… I didn’t know it, but E4X supports binding, just like Flex does for binding variables to other variables. Suddenly, my ValueObjects who already implement a toString method to ease debugging:

public function toString():String
{
        var s:String = "";
        s += "[class ProjectVO";
        s += " id=" + id;
        s += " projectName=" + projectName;
        s += " updatedDate=" + updatedDate;
        s += " creationDate=" + creationDate;
        s += " ]";
        return s;
}

Now have a nice way to get their XML equivalent:

public function toXML():XML
{
	var updatedDateString:String = DateUtils.getRubyDateString(updatedDate);
	var creationDateString:String = DateUtils.getRubyDateString(creationDate);
	var xmlified:XML =
		<project>
			<updated-date type="date">{updatedDateString}</updated-date>
			<creation-date type="date">{creationDateString}</creation-date>
			<id type="integer">{id}</id>
			<project-name>{projectName}</project-name>
		</project>;
	return xmlified;
}

Now, for my HTTPService, I can just do:

service.send ( myProjectVO.toXML() ) ;

It’s also really easy to update in case the Rail’s side changes for whatever reason. I usually can just copy from ServiceCapture, paste into the VO class since XML can now be written without quoting rules, and swap the values with bindings to the VO’s public properties… nice!

I don’t apologize for all the things I said, but wouldn’t have been so non-enthused had I known about bindings.

…so… uh… anyone got Remoting working for Rails yet? Or at least a WebService way of using Rails vs. frikin’ HTTPService?

13 Replies to “E4X Doesn’t Suck”

  1. cant you have common object definitions in the schemas and then just serilise the objects out and back again? I did a bit of this with c#

  2. Elegant solution.

    But what about remoting with Rails? How it should work at all? Is there special wrappers for that?

    Sorry if my questions are just stupid.

  3. Don’t know Campbell, don’t know enough about Rails yet.

    Don’t know Rostislav. I remember reading on OSFlash about a Remoting for Ruby project, but it aparently was abandoned. Bottom line, Ruby’d have to do what AMFPHP did and be able to understand the AMF protocol. Then, you could call controller methods like normal ActionScript methods passing parameters, including class instances that Ruby could understand.

    service.saveProject ( projectVO );

    Notice, the projectVO is an actual class instance in this case, and Ruby would have an equivalent ProjectVO.rb on the server-side to deserialize it to. Pipe dream….

  4. Dude that’d be sick! I wonder if there is ANYONE working on something like that. If not, I’m sure it’s going to come along. Maybe even as a Gem.

  5. Hey Jesse. I submitted an app to the Flex Derby competition that is RoR on the back end. If you haven’t tried the new toXml feature in RoR (on the ActiveRecord class) be sure to check it out… it is pretty sweet!

    I am going through the pain of parsing the XML using E4Xblahblah stuff in AS3; it isn’t my favorite b/c it feels so willy nilly… but it works

    Later man. Oh… here is the app…. http://mailfeeder.com/demo

  6. Yeah G, that’s how I’m getting the XML in the first place (per the tutorial). Is the app beta 2 or 3? She doesn’t work with 3 so I assume 2. :: goes to see if I still have the beta 2 player registered with PluginSwitcher ::

  7. Just got done updating it to work with Flash Player 9! It should be a go now. Not the most sophisticated UI, but does slice and dice a good bit of data… and RoR made it so sweet… even data paging was a BREEZZZZZZZE!

  8. BTW… we’re talking about doing a Ruby/RoR port of WebORB at TMC. Then you would have your…

    service.saveProject ( projectVO );

    …method call in Ruby. Now *that* would be sweet. :-)

  9. After a refresh, she works good. Man, that thing is fast… that sure seems like a lot of data going really really fast! I might have to ease up on my XML bashing after seeing that.

    Dude, if someone ports TMC to RoR, blog that shiz! That’d be insane to have a nice VO coding model like that WITHOUT Factories! :: drools ::

  10. Jesse,

    This old dog found new tricks with e4x.

    I have created an as3/xmxml documenter entirly in as3 Flex2. I only use php for file access.

    For the recursive directory parsing I send back XML.

    like;

    <root>
     <directory>
       <file>
        <directory>
          <file> ...... etc.

    When I first started looking at this data I siad, oh yeah what was the recursive loop I need to get all my file nodes.

    Well after further investigation, in e4x it’s this simple.

    var allFiles:XMLList = xml..files;

    Sorry man, you can’t say that sucks.

    Peace, Mike

    BTW, your code colors are blending into the white code background :)

  11. Hah! Good one. The title is E4X Doesn’t Suck. I’m still learning the syntax, but it’s really hard to get up the motivation to do when you spend 100% of your day doing nothing but dealing with data access from ColdFusion as nice serialized ActionScript objects. XML, to me, was a thing from the past; I just don’t get the opportunity to get passed raw data anymore.

    In the case of dealing with Ruby on Rails, I suddenly was back at square 1 again; there isn’t a Remoting for Ruby yet, so I’m stuck using XML, thus, I get the learn the syntax. I’m sure there is a lot more like the above; I know Darron Schall was all over it, I just couldn’t get excited about parsing XML. Just give me the frikin’ data, I shouldn’t have to parse anything!

    …but when you have to, I’m seeing how E4X is cool.

    As far as the site colors, yeah man, it’ll take all weekend to implement this new design.

  12. > serialized ActionScript objects.

    Hey I love remoting to, I use AMFPHP BUT… the transfer size!

    Due to the way my algorithm is in the documenter, I was FORCED to use XML. THe returned data was just to big. It was something I could not page either.

    For the rest of the project I use AMF. But, this app will be a Beta Apollo app ;-)

    I also had to use HTTPService for loading those 4000 line Adobe framework classes ;-)

    Everything else is AMF though. Is it AMFPHP that just has the return data limit or AMF in general?

    I just have to say, I have been following you for a long time and you have helped me out tremendously, just like a lot of other people.

    Face it Jesse, when you are a genious, you are bipolor, just manager it better. The depression phase you should turn into vacation. This is what I do now ;-)

    Peace, Mike

Comments are closed.