E4X Tutorial
E4X Tutorial
document.write(employees.person.(name == "Tove").age);
This example works in Firefox only!
To study these subjects first, find all the tutorials you need on our Home Page.
JavaScript = ECMAScript
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()
1
• C# Language
• International Character Sets
• Optical Disks
• Magnetic Tapes
• Data Compression
• Data Communication
• and much more
E4X How To
• With E4X, you can define an XML document as a JavaScript object.
• XML As a JavaScript Object
With E4X, you can declare an XML object the same way as you declare Date or Math objects: var x = new
XML();
var y = new Date();
var z = new Array();
Since you can declare an XML document as an XML object, it is also very easy to parse and manipulate the
XML document.
E4X Example
As an example, we can parse and edit an XML document that represents a note.
If we had this XML document stored in a string called note, we could load it into an XML object variable
called x, by writing the following JavaScript statement:var x = new XML(note);
Or we could assign the XML text directly to the XML object variable:
var x = new XML();
x=
<note>
<date>2002-08-01</date>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>;
Jani
Quite simple. Don't you think?
Why E4X?
• E4X makes XML very simple to use.
• E4X is Much Simpler
If you have ever tried to use JavaScript to parse and manipulate XML, you will find that E4X is much simpler
to use.
Without E4X you have to use an XML library (or an XML component) to work with XML.
2
These libraries or components have different syntax and work differently in different browsers.
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:Examplevar 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);
}
With E4X
The following example is the same as above but using E4X:var xmlDoc=new XML();
var xmlDoc=new XML();
xmlDoc.load("note.xml");
document.write(xmlDoc.body);
E4X Browsers
The browser support for E4X is limited.
Firefox
Firefox is currently the only browser with relatively good support for E4X.
In the next chapter there is a list of examples, describing some of the things you can do with E4x in Firefox.
Internet Explorer
So far there is no indication for of E4X support in Internet Explorer.
It looks like Microsoft is going to support its own alternative to AJAX.
AJAX is a new web development technique for creating interactive web applications using JavaScript, XML,
and CSS.
E4X Example
E4X makes scripting for XML very simple.
E4X Example
As an example, we will work with an XML document that represents an order.
3
<name>Maxilaku</name>
<qty>5</qty>
<price>155.00</price>
</item>
</order>
If we had this XML document stored in a string called txt, we could load it into an XML object variable called
order, by writing the following JavaScript statement:var order = new XML(txt);
Or we could assign the XML text directly to the XML object variable:
var order = new XML()
order=
<order id="555">
<date>2005-08-01</date>
<customer>
<firstname>John</firstname>
<lastname>Johnson</lastname>
</customer>
<item>
<name>Maxilaku</name>
<qty>5</qty>
<price>155.00</price>
</item>
</order>;
Source: http://w3schools.com/e4x/default.asp
By: DataIntegratedEntity