Filed under: actionScript, appCoreLib, development, e4x, flash, flex, regexp, xml
A powerful yet elegantly simple construct
As a flex developer, I consider XML as a necessary and highly useful language for various aspects of RIA development. There is a myriad of uses including but not limited to:
- external URL storage for links
- non-embedded content loading of jpgs, gifs, pngs and other swfs
- internal URL pointers for RESTful type services
- incoming service response objects
- application initialization data (e.g. dataProviders, default settings)
- and the list goes on
That is all well and good. But the problem is that if you don’t fully understand how to traverse an XML object and understand what it does when using e4x to access, then you are not making the most of this simple yet powerful development structure. The next problem lies in that there is very poor documentation on e4x in respect with the ActionScript XML class.
Truly understanding what the XML Object & e4x does
Adobe did a decent job of showing you how to traverse an XML object via e4x and then seeing their simple little trace statements. But what they failed to do was to give you useful examples of how to use that output and turn it into ActionScript-friendly objects.
Grabbing an XML node’s attribute
We have all probably used the following to get a value from an attribute from an XML node:
var o:Object = someXMLObject.@someAttribute; //o is now an XMLList
The issue with this example is that o is an instance of an XMLList. In order to fully make use of the value inside that singular attribute (why it turns it to an XMLList when specifying that node’s attribute expecting a singular value, I have no idea) we have to cast that value to what we want. Here are a few examples:
var s:String = String(someXMLObject.@someAttribute);
var n:Number = Number(someXMLObject.@someAttribute);
Easy!
Constructing an XML object from within ActionScript
Most everyone has probably used the following to construct an XML variable in AS3:
var x:XML =
<node someAttribute="value">
<subNode anotherAttribute="value"/>
<subNode>value</subNode>
</node>
Doing a trace on that would yield the expected XML to output to an XML string, retaining the nested structure and brackets. But using the new XML() constructor can yield some different results, some unexpected and not entirely useful.
var x:XML = new XML("node"); //traces "node" and not <node/>
So how would one dynamically construct a new XML object when not knowing the node name?
var x:XML = new XML("<" + dynamicValueForNodeName + "/>");
I don’t like the above setup. Its a messy read. Certainly there is another option?
var x:XML = <genereicNodeNameToBeChanged/>;
x.setName("newNodeName"); //traces <newNodeName/>
While this is documented, they really don’t touch on it at all in their given examples.
Setting values to nodes
If you wanted to put a subNode inside a parentNode you’d probably use syntax such as:
var x:XML = <node/>;
var x2:XML = <subNode/>;
x.appendChild(x2); //which yields <node><subNode/></node>
That is probably the most common usage of appendChild(). But did you know you can append values as well? Not just subNodes but also non-nodal values:
var x:XML = <node/>;
x.appendChild("someValue"); //traces <node>someValue</node>
Again, this is very useful but not really touched upon by the documentation.
Finding values using the . syntax
One of the nicest features about e4x is that you can rapidly find value(s) nested deep within the XML by using the “dot” syntax. I will abstain from listing an example here as there are numerous examples on the documentation site. But did you know that you can make multiple inquiries on an XML structure within the same line of code?
arrayElementType = String(item.metadata.(@name == "ArrayElementType").arg.(@key == "" || @key == "type").@value);
This eliminates the need to make multiline inquiries by getting an XMLList from one e4x boolean operation, then repeating down the line until you have driven down into the specific node you are looking for.
Using this new-found knowledge
Though you may or may not have known about some of these little tricks, you might be wondering, “so where do I use this”. A few posts back I eluded to the fact that I was developing an XML-to-ActionScript translator for AppCoreLib. The translator would then be able to quickly take in an XML response from a web service and spit back a useful ActionScript object of a specified class with little work involved by the responder. In order to do this, I have to make major use of XML and specifically the XML spit out by the describeType method. It means that I not only have to traverse the XML object, I also have to dynamically construct XML objects to be sent back. After the first build, I was a little miffed that I had some hard to read, and plain ol’ ugly ass code. I wanted to fully immerse myself in what the XML object and e4x had to offer.
If you have some not-so-well-documented tips and tricks for XML and e4x, send them along and I will add it to the post.
Filed under: riddles & puzzles
Describe or name the following: An object with only one edge and one surface.
Please refrain from posting your answers in the comments. Please post any questions you might have in trying to solve it. The answer will be posted in the answers sections next week.
Its been awhile since I last posted any of my not-so-weekly riddles. I really miss sharing them with you. So here is some food for thought.
Are there different sizes for infinity?
When this question was first posed to me, I almost immediately said no. How can one put a limit on a limitless concept? Or put another way, how can one comparatively assess the sizes of infinite things?
Check out Georg Cantor’s explanation – link
Filed under: actionScript, components, development, flex, jwo_lib
[UPDATE]
Thanks to Holmes (comments below) I had just enough info to use the proper search string. It is indeed called “mark occurrences”. And for those having a hard time finding where in the IDE it resides, here is a screen shot for you:

Since a few folks who have been trying to use jwoLib in Flex 3 projects have reported some funky bugs with the component set, I figured I would try to get in there and work them out.
I remember the last time I got on FB3 I was highly annoyed to find that every time you type something, it tries to highlight the code for you to show you other instances within the document. I also remember not being the only one annoyed as someone had posted how one might disable this pain in the rear. I just can’t seem to find the post or other relevant info on how to turn it off. Nor can I seem to find the option in the preferences. Can someone please refresh my memory?
Thanks,
J
Filed under: actionScript, components, development, flash, flex
I am in the process of working on a ClassUtil class that will have static methods for creating class instances from generic objects and xml objects. One thing that I use in order to create nested content within the outer-most object to be created is getDefinitionByName. If you are not familiar with this method check it out. It basically returns a Class object by provide the name of the class in a string format. You want a Class object of String, then you pass “String”. You want the Class object for com.somesite.model.vo.SomeVo, the you pass “com.somesite.model.vo.SomeVo” as the parameter and whallah! You just have to remember to use the fully qualified class path in the string parameter.
This technique combined with using describeType and others yields some pretty powerful results. More on that later tho. So the issue with using getDefinitionByName is that unless you have instantiated an instance of the class in question, your swf will contain the embedded class definition. Then you run into the run-time-error saying that the variable of that name cannot be found (or something like that).
The most common way to resolve this is to put a useless instance of the class somewhere in your application. Such as var foo:Foopher = null. Then the class definition of Foopher is embedded in the compiled swf. Someone please correct me if I am wrong in explaining this. The problem with this solution is it seems rather hacky. It just doesn’t settle well with me. So I sought a different path.
The more uncommon way to resolve this is to define your class with a static var like so:
package vo
{
import flash.net.registerClassAlias; [RemoteClass(alias="vo.NestedObject")] public class NestedObject {static private var _isRegistered:Boolean = false; public var id:uint; public var name:String; public function NestedObject () {if (!_isRegistered) {registerClassAlias("vo.NestedObject", NestedObject); _isRegistered = true;}}}
}
Now it may be that registerClassAlias is a bit expensive but seeing as it is done once and only once per class type, this shouldn’t pose a problem. The other benefit is that you don’t clutter unrelated classes or views with having to make dummy instances to get it to register. Lastly, you can pick and choose which classes to use this method on so as to limit the price of making the call.
Andre Michelle posted this on his blog. WhiteVoid is one of the sickest RIA interfaces I have ever had the luxery of screwing around with. Though its 3D, it doesn’t go overboard and instead presents enough depth perception to not interfere with user interaction. I love the use of blur effects too.
WhiteVoid – link
Filed under: mac
Just learned this cool tip for Mac users (thanks Mike O.!) So when you’re working unplugged and really want to optimize battery usage, you can invert the colors on your screen. Just push ctrl-option-propeller-8 and whalla! and inverted screen. So why is this so cool. Well think of it this way: Every non-black pixel requires power to illuminate it. If you use Eclipse, then most likely you are seeing lots of white. By inverting the color scheme, you have greatly reduced the number of white pixels showing, and in theory is using less power. Also the inversion of colors makes it easier to read in low-light situations.
The only issue that have is that it makes it very hard to find your cursor when over a large text input contol. But I am sure there is a simple fix to that too.
Cool find no?
Filed under: Uncategorized
Ever since subscribing to Scientific America(n) magazine, I have been super psyched about nanotechnology. They just had a recent article about proposed nanotechnology being used intravenously to do incredible things like monitoring insulin levels, detecting arterial blockages and many micro-intrusive counter measures to our species’ many ailments .
A nanometer is one billionth of a meter. Insanely small. Now scientist have made a break through in what appears to be “teaching” nanomaterials to bind, unbind and reshuffle via the guidance of DNA. Here is the original article – link
Any Superman fan can appreciate these possibilities.
Filed under: Uncategorized
Normally I wouldn’t put this kind of a post on my site but I am finding myself running into a bit of a time crunch with a personal project of mine. I am in need of a) a mentor or teacher and b) some good ol’ fashioned working, readable code.
Here’s the scoop. I need someone who has had experience working with PHP, MySQL and some sort of ORM framework. Specifically I need someone to teach me about the proper way of getting those 3 things working properly (e.g. integration in my current code base). Then I will try to get them to work with my AMFPHP/Flex RIA. However I may need some assistance in this as well.
Yes, I could spend hours researching this (which I have invested many hours doing so). But I know my limits when it comes to self-exploration and the opportunity costs in doing so are too high. Time is my most precious commodity. So are you interested? If so then read on. I don’t really care if you have 10+ years in the field or some high school kid, just so long as you know your stuff. All you need is to meet the following requirements:
- proficient in PHP & MySQL w/ knowledge of a ORM technology
- located in the USA (I don’t really want to deal with huge time differences)
- speak English proficiently
- the ability to articulate your points well so that a noob can understand them
- have a paypal account (I don’t want to screw around with cash, check , etc.)
- have skype (its free for skype to skype calls and a free download) or some IM client
- have maybe 4-8 hours to do this to start
If you meet the requirements send an email (no comments on the blog please) to jwopitz (at) gmail (dot) com with the following information:
- location in the US
- code samples for PHP (remember a php noob is reading this, but a developer too)
- skype and IM information
- price per hour for your help
I look forward to hearing from you.
I think when Yahoo first released their Flex 1.5 Map API, I was super psyched to get my hands on it. Of course I was just getting my hands dirty with Flex and found the integration and development process with Flex 1.0/1.5 was just awful.
Today an answer to one of my prayers has been sent. Yahoo’s Actionscript 3 Map API was just announced. I can’t wait to see where this leads. Not that there aren’t enough geospacial/mapping applications out there, but this will open the door to bigger and badder application development. Here is the original posting on Yahoo’s blog.
