One of the neat features I learned using Flex/Actionscript was e4x which stands for "ECMAScript for XML" . ECMAScript is what we commonly know as javascript. What's great about e4x is that its a javascript standard extension and that it's a really usefull and easy API.
Standards are great, especially ones like e4x!
I love learning standards based technologies because I know that I only need to learn it once and can use its implementation in many different places and situations. From my understanding it is currently supported in Firefox and Actionscript but, predictably not IE. But then who cares about IE :)
E4X is the new standard javascript way to access XML used in Flex
At first it can be a bit confusing as to how it works, but once you get the hang of it navigating your XML document becomes extremly easy. You can get a reference to an XML document by going:
var x = new XML(); or
var x = new XML(xmlstring);
You will no longer need that awful snippet of code to do browser detection to detemine how to create your parser for your XML documents. XML is one of the new data types introduced by e4x specification and represents an XML document. You will become familiar with this data type and its close relative XMLList.
XML parsing is now built-in to the language. Let say you have the following javascript code:
- var xmlDoc =<root>
- <person type="OperatingSystem">
- <name>Tux</name>
- <tech>Linux</tech>
- <goal>Choice</goal>
- <cost>0</cost>
- </person>
- <person type="ProgrammingLanguage">
- <name>Duke</name>
- <tech>Java</tech>
- <goal>Run anywhere</goal>
- </person>
- </root>;
- var x = new XML(xmlDoc);
- var people = x.person;
- for each (var person in people){
- alert(person.name+ "'s goal is "+ person.goal);
- }
- alert(x.person.(name=="Tux").tech);
- alert(x.person.(@type=="ProgrammingLanguage").tech)
- alert(x.person[1].name);
- x.person.(name=="Tux").goal="Have fun";
- alert(x.person.(name=="Tux").goal);
- x.person[0]+=<person type="OperatingSystem"><name>beastie</name><tech>BSD</tech>
- <goal></goal>security<cost>0</cost></person>
- alert(x);
Line 1: You can delcare xml in you code without having to treat is as a string. There are no quote marks (") around the xml document.
Line 15: First thing you will notice is that you can select from the XML document using the "." accessor/operator. Secondly the accessor will return a collection of selected elements of the type XMLList. XMLList is basically a collection of XML objects.
Line 16: E4X adds the for-each-in statement to the language which allows easy iteration over an XMLList. It returns an XML object for each item in the collection
Line 17: Will print out "Tux's goal is choice" and "Duke's goal is run anywhere". This shows basic access to the properties of the XML object. By default the "text()" fucntion is called if the resulting selection is a single element. i.e if there was more than one goal for a person an XMLList object would be returned instead of an XML object. This "blurring", as the spec calls it, between XMLList and XML is deliberate. An XML object is also an XMLList with 1 member.
Line 19: Will print "Linux". This show element selection by filtering on a text node value
Line 20: Will pirnt "Java". This show element selection by filtering on an attribute value
Line 21: Will output "Duke" This show element selection based on index.
Line 22+23: Set the goal element's text value to "Have fun" for the person element with name tux and print it out.
Line 24: Inserts a new person element into the XML document after the first person element.
You will notice there is no need to stipulate the root element. i.e no x.root.person just x.person. As you can see e4x also introduces several new datattype like XML,XMLList
An XMLList is an collection of XML elements that are selected by the e4x syntax. i.e It is a list of all person elements. A XMLList with a single element can be treated as an XML data type.
According to the spec all operations available to an XMLList object are available to the XML data type as, essentially an XML datatype is an XMLList with only one member in the collection.
After getting all excited about this ease of use the next problem you will encounter in the real world is namespaces. I hope to cover this in my next blog entry.