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


E4X Adds Direct XML Support to JavaScript : Page 4

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.




Full Text Search: The Key to Better Natural Language Queries for NoSQL in Node.js

Global Methods and Properties
Table 7 lists namespace global methods and properties with their descriptions and accompanying examples.

Methods/Properties Description Example
ignoreComments If ignoreComments is true, XML comments are ignored when constructing new XML objects.

Default -true

XML.ignoreComments = false;
var xmlElement = <author> <!-- info line --> Rahul Gupta </author>;
for each (var xmlElements in xmlElement.*)
alert(" Element = "+ xmlElements);

ignoreProcessingInstructions If ignoreProcessingInstructions is true, XML-processing instructions are ignored when constructing new XML objects.

Default -true

XML.ignoreProcessingInstructions= false;
var xmlElement = <publishing>
<?process author="yes"?>
<author type="freelance">Rahul Gupta </author>
for each (var xmlElements in xmlElement.*)
alert(" Element = "+ xmlElements);

ignoreWhiteSpace If ignoreWhitespace is true, insignificant whitespace characters are ignored when processing or constructing new XML objects. Whitespace characters are defined to be space (\u0020), carriage return (\u000D), line feed (\u000A), and tab (\u0009).

Default -true

XML.ignoreWhitespace = false;
var xmlElement =
<author>Rahul Gupta</author>

prettyIndent Child nodes will be indented relative to their parent node by the number of spaces specified by prettyIndent. This is effective only when prettyPrinting is true.

Default -2


XML.prettyIndent = 4;
var xmlElement =

prettyPrinting If prettyPrinting is true, the ToString and ToXMLString operators will normalize whitespace characters between certain tags to achieve a uniform appearance.

Default -true

var xmlElement = <publishing><author>Rahul Gupta </author>
<author>Jon </author></publishing>;


XML.prettyIndent = 4;
var xmlElement =
<publishing><author>Rahul Gupta </author>
<author>Jon </author></publishing>;

isXMLName(..) IsXMLName is used to determine whether it is a valid XML name that can be used as an element or attribute name.

//shows false
//shows true

Table 7. Namespace Global Methods and Properties

Advantages and Limitations of E4X
One more ECMAScript technique for handling XML is object mapping. You can map XML data onto a set of ECMAScript objects, manipulate those objects directly, and then map them back to XML. This approach has the advantage of allowing ECMAScript programmers to use their existing knowledge, but it has some problems. For example, an ECMA native object can't preserve the order of the original XML data. As E4X evolves, this approach will mature and become standardized. Table 8 summarizes the advantages and limitations of E4X.

Area Description
Browser Support As of now (Nov. 2006), Mozilla, FireFox 1.1 +, and Safari provide limited support. IE7 doesn't support E4X, but it is becoming standard so in future all browser should support it.
Code Size In general, the size of the code you produce when using E4X for a requirement tends to be smaller than the code generated with other XML-related technologies.
Learning Curve The learning curve is very low, as it is an extension to JavaScript.
Cost Both development and maintenance costs are lower than other related technologies such as XSLT, XML queries, or DOM routines.
Performance E4X is lighter weight than similar technologies so it has better performance.
Table 8. Advantages and Limitations of E4X

The author wishes to thank his colleagues Mr. Saurabh Bhatnagar, Tarun Gera, and Sumit Gupta for their valuable contributions as reviewers for this article.

Rahul Kumar Gupta has more than nine years' experience in the IT industry, particularly working with EAI, J2EE, design patterns, UML Java, RMI, Servlets, JSP, application servers, C, CORBA, WAP, and J2ME. He currently works with Indian IT company HCL Technologies Limited, NOIDA (India).
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