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 is similar to 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, we can load it into an XML object variable named x by writing the following JavaScript statement:

var x = new XML(note)

Or we can directly assign XML text 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 on XML documents is very simple.

Write a JavaScript statement as shown in the above example:

document.write(x.from)

The output will be:

John

Very simple. What do you think?