11

I'm trying to figure this out, but there's not much information. Which browsers support E4X, and why isn't it more widely adopted?

ChrisOh
  • 289
  • 1
  • 3
  • 8

4 Answers4

10

Which browsers support E4X

Firefox and others based on the Mozilla codebase.

why isn't it more widely adopted?

Because it offers little practical functionality not already covered by existing standards such as DOM.

OK, it's simpler to use than DOM, but as the price for that you don't get access to all the features of XML, and the thoroughly idiotic, needless XML literal/template syntax is a security disaster, making it so authors of even completely static htaccess-protected documents have to worry about working around the feature.

As a simpler method for accessing the results of an XMLHttpRequest, JSON totally won. For full-on XML processing, you still need DOM. For easier document handling, there are selectors, XPath and JS libraries that can do it without having to introduce weird new language syntax.

That doesn't leave much of a niche for E4X. TBH I wish it would die. (ETA: it has now pretty much done so.)

bobince
  • 528,062
  • 107
  • 651
  • 834
  • 1
    I got used to e4x in flash, and have been wondering why it isn't supported outside Firefox. Thanks and +1. – Amarghosh Feb 13 '10 at 13:09
  • E4X is really useful syntax. The way you describe it you'd argue RegExp are terrible as well. They serve their purpose. Another thing that bugs me since I'm ranting, is why does there only have to be one way to do things? Why can't I chose to use XML instead of forced to use JSON? I use both btw. This is what I dislike about the web community right now. Also, @bobince the link you posted to is no longer valid. :( – 1.21 gigawatts Nov 11 '15 at 01:36
  • Updated link following Google Code archiving. And yes, I believe JS RegExp literal syntax was also a mistake, albeit one without the same negative impact as E4X had. The problem you have without regex literals is mainly the extra layer of backslash-escaping you need to put them in a string literal. Other languages have solved this in a more generic and flexible way with 'raw' string literal format. – bobince Nov 11 '15 at 14:32
  • The answer should probably be updated in light of Firefox 16/21 (through Gecko and SpiderMonkey) dropping E4X support. – gsnedders Jan 26 '16 at 00:25
4

Firefox dropped E4X support in version 16:

E4X is deprecated. It will be disabled by default for content in Firefox 16, disabled by default for chrome in Firefox 17, and removed in Firefox 18. Use DOMParser/DOMSerializer or a non-native JXON algorithm instead.

Darth Egregious
  • 18,184
  • 3
  • 32
  • 54
  • This is really very surprising that this happened, considering [React](https://reactjs.org)'s [JSX](https://reactjs.org/docs/introducing-jsx.html) is just [E4X](https://en.wikipedia.org/wiki/ECMAScript_for_XML) done in JS. – bobobobo Oct 07 '19 at 15:00
3

According to w3schools, "Firefox is currently the only browser with relatively good support for E4X."

You could try XPath instead. Although XPath isn't cross-browser there are several Javascript solutions for it like this jQuery plugin.

EDIT

You could actually use jQuery without a plugin for this:

$('<xml><some><code>code</code><tag>text</tag></xml></xml>').find('some > code').text()
Harmen
  • 22,092
  • 4
  • 54
  • 76
0

I have developed a babel plugin that adds E4X basic support to all browsers via babel compilation.

https://www.npmjs.com/package/babel-plugin-transform-simple-e4x

You can also use the npm simple4x library to parse xml strings to a XML like object.

https://www.npmjs.com/package/simple4x

The plugin transpiles the following E4X:

var fooId = 'foo-id';
var barText = 'bar text';
var xml =
    <xml>
        <foo id={fooId}>
          {barText}
        </foo>
    </xml>;

To the following JavaScript:

var XML = new require("simple4x");

var fooId = 'foo-id';
var barText = 'bar text';
var xml = new XML("<xml><foo id=\"" + fooId + "\">" + barText + "</foo></xml>");