ECMAscript for XML
This article or section is currently under construction
In principle, someone is working on it and there should be a better version in a not so distant future.
If you want to modify this page, please discuss it with the person working on it (see the "history")
ECMAscript for XML (ak E4X) is an extension to ECMAscript.
It is available in
- Some JavaScript engines, e.g.
- Firefox SpiderMonkey (the default JS engine)
- ActionScript 3
Using E4X is considerably simpler than using the DOM. Maybe some day we will be back to the elegance of languages like LISP instead of fighting against obscure C/Java syntax.....
Usage patterns
Creating an E4X XML object
(1) The easiest way is to create a new variable and just create the XML structure like this. In ECMAScript 4 (e.g. ActionScript 3) you should define the type of the variable.
var instructions:XML = <stepbystep> <doctitle></doctitle> <steps> <title></title> </steps> </stepbystep>;
Don't forget the ";" at the end since this instruction extends over several lines.
(1b) In JavaScript 1.6 (ECMAScript 3) you can't define types and you simply use:
var instructions = <stepbystep> ....
This the same principle as creating an array or an object like this:
var arr = [item1,item2,item3]; var obj = {a:"item 1",b:"item 2",c:"item 3"}
(2) You also can create an XML E4X object from a string with the new XML() constructor.
var instruction_string = "<stepbystep>
<doctitle></doctitle> <steps> <title></title> </steps> </stepbystep>";
var instruction2:XML = new XML(instruction_string);
(3) Computing expressions
to do: {...}
XML lists
XML lists are the other important datastructure in E4X. Lists are needed for example to hold results of queries (like NodeLists in the DOM model).
Here is a little example of an XML list:
<step> <title>step 1</title> </step> <step> <title>step 2</title> </step> <step> <title>step 3</title> </step>
Accessing elements and attributes
Again, there is a similarity between E4X objects, arrays, and traditional Object.
- (1) Using the "." syntax
Let's take an example:
var steps:XML = <stepbystep> <doctitle>Sample document</doctitle> <info> <para>See <a href="http://edutechwiki.unige.ch/en/ECMAscript_for_XML">ECMAscript for XML</a></para> </info> <steps> <title>List of steps</title> <step> <title>step 1</title> </step> <step> <title>step 2</title> </step> <step> <title>step 3</title> <para>That was easy !</para> </step> </steps> </stepbystep>;
A little example
- Story (look at the source of this file)
Links
Overviews
- ECMAScript for XML (Wikipedia)
Introductions and tutorials
- JavaScript
- Fremantle, Paul and Anthony Elder (1005). AJAX and scripting Web services with E4X, Part 1, IBM Works, HTML
- E4X Tutorial, W3Schools.
- Introducing E4X (by Kurt Cagle, xml.com, nov 2007)
- E4X by Mark. Useful as short manual !
- At Mozilla (Firefox)
- E4X Tutorial (Mozilla Developer center)
- Processing XML with E4X (JavaScript 1.6 +)
- E4X, Mozilla Developer Center.
- Flash/ActionScript/Flex
- The E4X approach to XML processing (Adobe, part of the Flex manual/Programming ActionScript 3.0)
- E4X (Adobe, Flex, Getting Started)
- Intro to E4X (Actionscript.org) by Hasan Otuome
- Php
- PHP5 quick database E4X queries (Adobe, Flex cookbook) by Sand Wyrm.
- Namespaces
- How does E4X deal with XML with namespaces? by Martin Honnen (2005)
- How can I access elements or attributes that are in a namespace? by Martin Honnen (2005)
Standard and Manuals
- ECMA-357 standard
- E4X Short tutorial by Mark. Also useful as short manual !