Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


E4X Adds Direct XML Support to JavaScript : Page 2

E4X provides a more intuitive, familiar, and natural way for JavaScript developers to deal with XML. E4X code is easier to read, write, and maintain than standard XML in programming models such as XSLT, XQuery, and the DOM.




Application Security Testing: An Integral Part of DevOps

Understanding the Usage of E4X
Before digging deeper into E4X, see how E4X can make a difference in your XML handling. Table 1 provides examples of two XML parsing approaches: one using DOM, the other E4X.

The format of the sample.xml in Table 1 is as follows:

<publishing> <description>List of authors and Articles</description> <author type="freelance" books="java"> <name>Rahul Gupta</name> </author> <author type="employed"> <name>Jon</name> </author> </publishing>

The output from both approaches is the same (see Figure 1).

Figure 1. The Output from XML Parsing Approaches (With and Without E4X) Is the Same

E4X offers a cleaner and easier way to retrieve values from XML within JavaScript, wouldn't you say? All because E4X is meant specifically for providing XML support. Without E4X, you have to use an XML library/component, and those work differently in different browser.

E4X also provides all types of language constructs, such as statement loops, operators, namespaces, objects, keywords, etc. In order to use it in your pages, you just add the parameter e4x=1 in the script tag as follows:

<script type="application/x-javascript; e4x=1">


<script type="text/javascript; e4x=1">

Context Keywords
E4X adds three context keywords (each, XML, namespace). Context keywords take on a specific meaning when used in specified contexts where identifiers are not permitted by the syntactic grammar.

The default XML namespace of the global scope has the initial value of no namespace. You can define its scope also. For example, if you define it within a function call, the value set will not be accessible outside the function. To deal with XML with namespaces easily, you can declare a default namespace for the current scope (e.g., function scope or global scope) using the newly introduced statement, as follows:

default xml namespace = expression;

The above statement should evaluate to a string with a URI or to a namespace object. The result is that all elements created in the current scope will be in the default namespace, provided they have no prefix in their name. The following code snippet shows the uri value when the default namespace is both defined and not defined:

var xmlElement1 = <p>Hello how are you</p>; // shows ''; alert(xmlElement1.name().uri); // set default namespace default xml namespace = 'http://www.w3.org/1999/xhtml'; var xmlElement2 = <p>Hello how are you</p>; // shows 'http://www.w3.org/1999/xhtml'; alert(xmlElement2.name().uri);

The for-in statement evaluates an expression and iterates through each property of the resulting XML object. When the expression evaluates to an XML object, the for-in statement converts the XML object to an XMLList and iterates over the resulting XMLList. The following snippet shows the usage of a for-in loop statement:

var xmlElement = <publishing> <author type="freelance" books="java">Rahul Gupta</author> <author type="employed">Jon</author> </publishing>; //show Rahul Gupta, freelance, Jon, employed for (var childElement in xmlElement.author) { alert("Element No ["+childElement +"] = " + xmlElement.author[childElement]); for (var childAttribute in xmlElement.author[childElement].@type) alert("Attribute No ["+childAttribute +"] = " + xmlElement.author[childElement].@type); }

The for-each-in statement iterates through each property of the resulting XMLList object in order. The following code snippet shows the usage of for-each-in statement:

var xmlElement = <publishing> <author type="freelance" books="java">Rahul Gupta</author> <author type="employed">Jon</author> </publishing>; //shows Rahul Gupta, freelance, Jon, employed for each (var xmlElements in xmlElement.author) { alert( " Element = "+ xmlElements); for each (var xmlAttributes in xmlElements.@type) alert( " Attribute = "+ xmlAttributes.toXMLString()); }

The for-each-in statement behaves differently from the for-in statement. In particular, it assigns the loop variable over the range of the object rather than the domain of the object. For example, the for-each-in statement binds the loop variable to the property values of the given object rather than the property names.

Identifiers and Punctuators
Table 2 lists E4X identifiers and punctuators with descriptions and accompanying examples.

Identifiers Sign/Represented By Description Example
Attribute Identifiers @ An attribute identifier is used to identify the name of a XML attribute. It evaluates to a value of type AttributeName. The preceding "@" character distinguishes an XML attribute from an XML element with the same name.

var xmlElement = <publishing>
<author type="freelance">Rahul Gupta</author>
// shows 'freelance'

Qualified Identifiers :: A qualified identifier is used to identify values defined within a specific namespace. They may be used to access, manipulate, and create namespace-qualified XML elements and attribute names.

var xmlElement3 = <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<a href="http://www.google.com"> Google </a>
</body> </html>
var xhtml = new Namespace('http://www.w3.org/1999/xhtml');
var xhtmlElements = xmlElement3.xhtml::*;

//shows 1 - for body

Wildcard Identifiers * A wildcard identifier is used to identify any name. It may be used for matching namespaces, properties of XML objects, or XML attributes.

var xmlElement = <publishing>
<author type="freelance" books="java">Rahul</author>
<author type="employed">Jon</author>
//shows all elements and Attributes
for each (var xmlElements in xmlElement.*) {
alert("Element="+ xmlElements);
for each (var xmlAttributes in xmlElements.@*)

Descendent Accessor .. A descendent accessor supports the XML descendent (Child, grandchild, great-grandchild) accessor.

var xmlElement =
<author type="freelance">Rahul Gupta</author>
<author type="employed">Jon</author>
// shows 'freelanceemployed'

Table 2. E4X Identifiers and Punctuators

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date