JavaScript and XML : Part 3 : Page 2 | JavaScript: The Definitive Guide, Fifth Edition | WebReference

JavaScript and XML : Part 3 : Page 2 | JavaScript: The Definitive Guide, Fifth Edition


[previous] [next]

JavaScript and XML: Part 3

By  David Flanagan

21.8 E4X: ECMAScript for XML

ECMAScript for XML, better known as E4X, is a standard extension* to JavaScript that defines a number of powerful features for processing XML documents. At the time of this writing, E4X is not widely available. Firefox 1.5 supports it, and it is also available in version 1.6 of Rhino, the Java-based JavaScript interpreter. Microsoft does not plan to support it in IE 7, and it is not clear when or whether other browsers will add support.

Although it is an official standard, E4X is not yet widely enough deployed to warrant full coverage in this book. Despite its limited availability, though, the powerful and unique features of E4X deserve some coverage. This section presents an overview-by-example of E4X. Future editions of this book may expand the coverage.

The most striking thing about E4X is that XML syntax becomes part of the JavaScript language, and you can include XML literals like these directly in your JavaScript code:

The XML literal syntax of E4X uses curly braces as escape characters that allow you to place JavaScript expressions within XML. This, for example, is another way to create the XML element just shown:

In addition to this literal syntax, you can also work with XML parsed from strings. The following code adds another element to your periodic table:

When working with XML fragments, use XMLList() instead of XML():

* E4X is defined by the ECMA-357 standard. You can find the official specification at https://www.ecmainternational.org/publications/standards/Ecma-357.htm.


[previous] [next]

URL: