E4X How To

With E4X, you can define XML documents as JavaScript objects.

E4X Example

As an example, we can parse and edit an XML document representing a note.

This XML document looks like this:

<note>
<date>2008-08-08</date>
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>

If we store this XML document in a string named note, then by writing the following JavaScript statement, we can load it into an XML object variable named x:

var x = new XML(note)

Or we can directly use XML text to assign a value to the XML object variable:

var x = new XML()
x=
<note>
<date>2008-08-08</date>
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>

XML is a JavaScript object

With E4X, you can declare XML objects like you declare Date or Math objects:

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

Since XML documents can be declared as XML objects, parsing and operating XML documents is very simple.

Following the example above, write a JavaScript statement:

document.write(x.from)

Will output:

John

Very simple. What do you think?