XML - E4X
E4X adds direct support for XML to JavaScript.
E4X Example
<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);
This example only works in Firefox!
XML as a JavaScript Object
E4X is an official JavaScript standard that adds direct support for XML.
With E4X, you can declare XML object variables in 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 an extension to JavaScript that adds direct support for XML. ECMA-357 (E4X) was standardized in June 2004.
The ECMA organization (founded in 1961) is dedicated to standardization for information and communication technology (ICT) and consumer electronics (CE). Standards established by ECMA include:
- JavaScript
- C# language
- International character sets
- Optical discs
- Magnetic tape
- Data compression
- Data communication
- etc.
Without E4X
The following example is a cross-browser example. It loads an existing XML document ("note.xml") into an XML parser and displays a message:
Example
//code for Internet Explorer
if (window.ActiveXObject)
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load("note.xml");
displaymessage();
}
// code for Mozilla, Firefox, etc.
else (document.implementation && document.implementation.createDocument)
{
xmlDoc= document.implementation.createDocument("","",null);
xmlDoc.load("note.xml");
xmlDoc.onload=displaymessage;
}
function displaymessage()
{
document.write(xmlDoc.getElementsByTagName("body")[0].firstChild.nodeValue);
}
Try it Yourself »
Using E4X
The following example is the same as the example above, but uses E4X:
xmlDoc.load("note.xml");
document.write(xmlDoc.body);
Much simpler, isn't it?
Browser Support
FirefoxIt is currently the only browser with relatively good support for E4X.
Currently, those that do not support E4X include:Opera、ChromeorSafari。
So far, there is no indicationInternet Explorerof support for E4X.
The Future of E4X
E4X has not gained widespread support. Perhaps it provides too few practical features and has not yet been addressed by other solutions:
- For complete XML processing, you also need to learnXML DOMandXPath
- For accessing XMLHttpRequests,JSONis the preferred format.
- For simple document processing,JQuerythe choice is easier.
Other Extensions