E4X: ECMAScript for XML

ECMAScript for XML, better known as E4X, is a standard extension[22] to JavaScript that defines a number of powerful features for processing XML documents. E4X is supported by Spidermonkey 1.5 and Rhino 1.6. Because it is not widely supported by browser vendors, E4X may perhaps be best considered a server-side technology for script engines based on Spidermonkey or Rhino.

E4X represents an XML document (or an element or attribute of an XML document) as an XML object and represents XML fragments (more than one XML element not included in a common parent) with the closely related XMLList object. We’ll see a number of ways to create and work with XML objects throughout this section. XML objects are a fundamentally new kind of object, with (as we’ll see) much special-purpose E4X syntax supporting them. As you know, the typeof operator returns “object” for all standard JavaScript objects other than functions. XML objects are as different from ordinary JavaScript objects as functions are, and the typeof operator returns “xml”. It is important to understand that XML objects are unrelated to the DOM (Document Object Model) objects used in client-side JavaScript (see Chapter 15). The E4X standard defines optional features for converting between the E4X and DOM representations of XML documents and elements, but Firefox does not implement these. This is another reason that E4X may be best considered a server-side technology.

This section presents a quick tutorial on E4X ...

Get JavaScript: The Definitive Guide, 6th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.