XML e4x

## XML Basic

XML HOME XML Introduction XML How to use XML Tree XML Syntax XML Elements XML Attributes XML Validation XML Validator XML Viewing XML CSS XML XSLT

## XML JavaScript

XML HTTP Request XML Parser XML DOM XML in HTML XML Applications

## XML Advanced

XML Namespaces XML CDATA XML Encoding XML Server XML DOM Advanced XML Technologies XML in Real Life XML Editors XML E4X XML Summary

## XML Examples

XML Examples XML Quiz XML Certificate

# XML - E4X

« Previous
Next Chapter »

E4X adds direct support for XML to JavaScript.

## E4X Example

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);

This example works in Firefox only!

Try it yourself »

## XML As a JavaScript Object

E4X is an official JavaScript standard that adds direct support for XML.

With E4X, you can declare an XML object variable the same way as you declare a Date or an Array object variable:

var x = new XML()
var y = new Date()
var z = new Array()

## E4X is an ECMAScript (JavaScript) Standard

ECMAScript is the official name for JavaScript. ECMA-262 (JavaScript 1.3) was standardized in December 1999.

E4X is an extension of JavaScript 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 Communication Technology (ICT) and Consumer Electronics (CE). ECMA has developed standards for:

- JavaScript
- C# Language
- International Character Sets
- Optical Disks
- Magnetic Tapes
- Data Compression
- Data Communication
- and much more...

## Without E4X

The following example is a cross browser example that loads an existing XML document ("note.xml") into the XML parser and displays the message from the note:

## Example

var xmlDoc;
//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 »

## With E4X

The following example is the same as above but using E4X:

var xmlDoc=new XML();
xmlDoc.load("note.xml");
document.write(xmlDoc.body);

Much simpler, isn't it?

## Browser Support

Firefox is currently the only browser with relatively good support for E4X.

There are currently no support for E4X in Opera, Chrome, or Safari.

So far there is no indication for of E4X support in Internet Explorer.

## The Future of E4X

E4X is not widely supported. Maybe it offers too little practical functionality not already covered by other solutions:

- For full XML processing, you still need the XML DOM and XPath
- For accessing XMLHttpRequests, JSON is the preferred format.
- For easy document handling, JQuery selectors are easier.

« Previous
Next Chapter »

## Your suggestion:

Your E-mail (optional):

Page address:

Description:

Close [X]

## Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

## WEB HOSTING

Best Web Hosting UK Reseller Hosting Wow! $5 SSD Hosting

## WEB BUILDING

Download XML Editor
FREE Website BUILDER FREE Website Creator

## WEB RESOURCES

Web Statistics Web Validation

## SHARE THIS PAGE