W3Schools

home HOME

E4X Tutorial
E4X Home
E4X HowTo
E4X Why?
E4X Browsers
E4X Example

Selected Reading
Web Statistics
Web Glossary
Web Hosting
Web Quality

W3Schools Forum

Helping W3Schools

E4X How To

home next

With E4X, you can define an XML document as a JavaScript object.


E4X Example

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

The XML document looks like this:

<note>
<date>2002-08-01</date>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</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>


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.

For the XML example above, the JavaScript statement:

document.write(x.from)

will produce the output:

Jani

Quite simple. Don't you think?


home next


Learn XML with Stylus Studio XML Editor – Free Download

Stylus Studio provides tools for editing and debugging XML, XSLT, XML Schema, DTD, XQuery, Web Services,and more!

Using Stylus Studio XML Tools You Can:

See why millions use Stylus Studio as their preferred XML tool. Download a FREE TRIAL or watch an online VIDEO DEMO today!




Jump to: Top of Page or HOME or Printer Friendly Printer friendly page

W3Schools provides material for training only. We do not warrant the correctness of its contents. The risk from using it lies entirely with the user. While using this site, you agree to have read and accepted our terms of use and privacy policy.

Copyright 1999-2008 by Refsnes Data. All Rights Reserved.

Validate Validate W3C-WAI level A conformance icon W3Schools was converted to XHTML in December 1999