XML - E4X
E4X adds direct support for XML to JavaScript.
Example
<html><!DOCTYPE html> <html> <body> <p>This example works in Firefox only.</p> <script> var employees= <employees> <person> <name>Tove</name> <age>32</age> </person> <person> <name>Jani</name> <age>26</age> </person> </employees>; document.write(employees.person.(name == "Tove").age); </script> </body> </html>
This example only works with Firefox!
Run Example»Click the "Run Example" button to view the online example
XML as a JavaScript object
E4X is a formal JavaScript standard that adds direct support for XML.
Using E4X, you can declare XML object variables the same way you declare Date or Array object variables:
var y = new Date()
var z = new Array()
E4X is an ECMAScript (JavaScript) standard
ECMAScript Is the official name of JavaScript. ECMA-262 (JavaScript 1.3) was standardized in December 1999.
E4X is a JavaScript extension that adds direct support for XML. ECMA-357 (E4X) was standardized in June 2004.
The ECMA organization (founded in 1961) is dedicated to the standardization of information and communications technology (ICT) and consumer electronics (CE). The standards set by ECMA are:
JavaScript
C# Language
International Character Set
CD
Tape
Data compression
Data communication
Wait...
Not using E4X
The following example is a cross-browse An instance of the parser, which loads an existing XML document ("note.xml") into the XML parser and displays the message:
Instance
<html> <head> <script> var xmlDoc; function loadXML() { //load xml file code for IE if (window.ActiveXObject) { xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async=false; xmlDoc.load("note.xml"); displaymessage(); } // code for Mozilla, etc. else if (document.implementation && document.implementation.createDocument) { xmlDoc= document.implementation.createDocument("","",null); xmlDoc.load("note.xml"); xmlDoc.onload=displaymessage; } else { document.write("Your browser cannot handle this script"); } } function displaymessage() { document.write(xmlDoc.getElementsByTagName("body")[0].firstChild.nodeValue); } </script> </head> <body onload="loadXML()"> </body> </html>
Run Example»
Click the "Run Example" button to view the online example
Use E4X
The following example is the above example Same, but using E4X:
xmlDoc.load("note.xml");
document.write(xmlDoc.body);
Much simpler, isn't it?
Browser support
Firefox is currently the only browser that has good support for E4X.
Currently, E4X is not supported by Opera, Chrome or Safari.
So far, there is no sign of support for E4X in Internet Explorer.
The Future of E4X
E4X does not have widespread support. Perhaps it provides too few practical features and has not been covered by other solutions:
For complete XML processing, you also need to learn XML DOM and XPath
JSON is the preferred format for accessing XMLHttpRequests.
For simple document processing, JQuery is the easier choice.