NextGen Knowledge Center

About E4X

ECMAScript for XML (E4X), introduced in JavaScript 1.6, is a JavaScript extension that provides native XML support to ECMAScript:

var person1 = new XML("<person></person>");
var person2 = <person></person>;

E4X supplies a simpler alternative to Document Object Model (DOM) interfaces for accessing XML documents. E4X also offers a new way to make XML visible. Prior to E4X, XML had to be accessed at an object level. E4X regards XML as primitive level, which suggests quicker access, improved support, and acknowledgment as a component (data structure) of a program. Provided below are several useful XML object methods:

  • appendChild()—appends a child element
  • name()—gets the name of an element
  • attribute()—gets an attribute of an element
  • children()—gets a list of all of an element's child elements
  • length()—gets the count of an element's child elements.

Use DOM-like syntax to access XML elements and use @ for element attributes:

<person>
      <name>
            <first/>
            <last/>
      </name>
<address type="home"/>
</person>
person.name.first = "Joe"
person['name']['first'] = "Joe"
…
person.address.@type = "work";
person['address']['@type'] = "work";