Saturday, August 25, 2012

XML - E4X


E4X adds direct support for XML to JavaScript.

E4X Example

var employees=
<employees>
<person>
    <name>Tove</name>
    <age>32</age>
</person>
<person>
    <name>Jani</name>
    <age>26</age>
</person>
</employees>;

document.write(employees.person.(name == "Tove").age);
This example works in Firefox only!
Try it yourself »


XML As a JavaScript Object

E4X is an official JavaScript standard that adds direct support for XML.

With E4X, you can declare an XML object variable the same way as you declare a Date or an Array object variable:

var x = new XML()

var y = new Date()

var z = new Array()

E4X is an ECMAScript (JavaScript) Standard

ECMAScript is the official name for JavaScript. ECMA-262 (JavaScript 1.3) was standardized in December 1999.

E4X is an extension of JavaScript that adds direct support for XML. ECMA-357 (E4X) was standardized in June 2004.

The ECMA organization (founded in 1961) is dedicated to the standardization of Information and Communication Technology (ICT) and Consumer Electronics (CE). ECMA has developed standards for:
  • JavaScript
  • C# Language
  • International Character Sets
  • Optical Disks
  • Magnetic Tapes
  • Data Compression
  • Data Communication
  • and much more...

Without E4X

The following example is a cross browser example that loads an existing XML document ("note.xml") into the XML parser and displays the message from the note:

Example

var xmlDoc;
//code for Internet Explorer
if (window.ActiveXObject)
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load("note.xml");
displaymessage();
}
// code for Mozilla, Firefox, etc.
else (document.implementation && document.implementation.createDocument)
{
xmlDoc= document.implementation.createDocument("","",null);
xmlDoc.load("note.xml");
xmlDoc.onload=displaymessage;
}

function displaymessage()
{
document.write(xmlDoc.getElementsByTagName("body")[0].firstChild.nodeValue);
}

Try it yourself »

With E4X

The following example is the same as above but using E4X:

var xmlDoc=new XML();
xmlDoc.load("note.xml");
document.write(xmlDoc.body);

Much simpler, isn't it?

Browser Support

Firefox is currently the only browser with relatively good support for E4X.

There are currently no support for E4X in OperaChrome, or Safari.

So far there is no indication for of E4X support in Internet Explorer.

The Future of E4X

E4X is not widely supported. Maybe it offers too little practical functionality not already covered by other solutions:
  • For full XML processing, you still need the XML DOM and XPath
  • For accessing XMLHttpRequests, JSON is the preferred format.
  • For easy document handling, JQuery selectors are easier.

XML Editors


If you are serious about XML, you will benefit from using a professional XML Editor.

XML is Text-based

XML is a text-based markup language.

One great thing about XML is that XML files can be created and edited using a simple text-editor like Notepad.

However, when you start working with XML, you will soon find that it is better to edit XML documents using a professional XML editor.

Why Not Notepad?

Many web developers use Notepad to edit both HTML and XML documents because Notepad is included with the most common OS and it is simple to use. Personally I often use Notepad for quick editing of simple HTML, CSS, and XML files.

But, if you use Notepad for XML editing, you will soon run into problems.

Notepad does not know that you are writing XML, so it will not be able to assist you.

Why an XML Editor?

Today XML is an important technology, and development projects use XML-based technologies like:
  • XML Schema to define XML structures and data types
  • XSLT to transform XML data
  • SOAP to exchange XML data between applications
  • WSDL to describe web services
  • RDF to describe web resources
  • XPath and XQuery to access XML data
  • SMIL to define graphics
To be able to write error-free XML documents, you will need an intelligent XML editor!

XML Editors

Professional XML editors will help you to write error-free XML documents, validate your XML against a DTD or a schema, and force you to stick to a valid XML structure.
An XML editor should be able to:
  • Add closing tags to your opening tags automatically
  • Force you to write valid XML
  • Verify your XML against a DTD
  • Verify your XML against a Schema
  • Color code your XML syntax


At http://www.nabeeljamil.blogspot.com/ we have been using XMLSpy for many years. XMLSpy is our favorite XML editor. These are some of the features we especially like:
  • Now available in 32-bit and 64-bit versions
  • Easy to use
  • Context-sensitive entry helpers
  • Syntax coloring and pretty printing
  • Built in validation & well-formedness checking
  • Easy switching between text view and grid view
  • Graphical XML Schema editor
  • Database import and export for all major databases
  • SharePoint Server support
  • Built in templates for many XML document types
  • Chart creation for displaying XML data
  • Intelligent XPath 1.0/2.0 auto-completion
  • XSLT 1.0/2.0 editor, profiler, and debugger
  • XQuery editor, profiler, and debugger
  • SOAP client and debugger
  • Graphical WSDL 1.1/2.0 editor
  • XBRL validation & taxonomy editing
  • Support for Office 2007 / OOXML
  • Code generation in Java, C++, and C#
  • New! HTML5 and CSS3 support

XMLSpy is just one of the six tools in the Altova MissionKit® XML software suite.
Read more about the Altova MissionKit for XML Developers.

XML in Real Life


Some examples of how XML can be used to exchange information.

Example: XML News

XMLNews is a specification for exchanging news and other information.

Using such a standard makes it easier for both news producers and news consumers to produce, receive, and archive any kind of news information across different hardware, software, and programming languages.

An example XMLNews document:

<?xml version="1.0" encoding="ISO-8859-1"?>
<nitf>
  <head>
    <title>Colombia Earthquake</title>
  </head>
  <body>
    <headline>
      <hl1>143 Dead in Colombia Earthquake</hl1>
    </headline>
    <byline>
      <bytag>By Jared Kotler, Associated Press Writer</bytag>
    </byline>
    <dateline>
      <location>Bogota, Colombia</location>
      <date>Monday January 25 1999 7:28 ET</date>
    </dateline>
  </body>
</nitf>


Example: XML Weather Service

An example of an XML national weather service from NOAA (National Oceanic and Atmospheric Administration):

<?xml version="1.0" encoding="ISO-8859-1" ?>
<current_observation>

<credit>NOAA's National Weather Service</credit>
<credit_URL>http://weather.gov/</credit_URL>

<image>
  <url>http://weather.gov/images/xml_logo.gif</url>
  <title>NOAA's National Weather Service</title>
  <link>http://weather.gov</link>
</image>

<location>New York/John F. Kennedy Intl Airport, NY</location>
<station_id>KJFK</station_id>
<latitude>40.66</latitude>
<longitude>-73.78</longitude>
<observation_time_rfc822>Mon, 11 Feb 2008 06:51:00 -0500 EST
</observation_time_rfc822>

<weather>A Few Clouds</weather>
<temp_f>11</temp_f>
<temp_c>-12</temp_c>
<relative_humidity>36</relative_humidity>
<wind_dir>West</wind_dir>
<wind_degrees>280</wind_degrees>
<wind_mph>18.4</wind_mph>
<wind_gust_mph>29</wind_gust_mph>
<pressure_mb>1023.6</pressure_mb>
<pressure_in>30.23</pressure_in>
<dewpoint_f>-11</dewpoint_f>
<dewpoint_c>-24</dewpoint_c>
<windchill_f>-7</windchill_f>
<windchill_c>-22</windchill_c>
<visibility_mi>10.00</visibility_mi>

<icon_url_base>http://weather.gov/weather/images/fcicons/</icon_url_base>
<icon_url_name>nfew.jpg</icon_url_name>
<disclaimer_url>http://weather.gov/disclaimer.html</disclaimer_url>
<copyright_url>http://weather.gov/disclaimer.html</copyright_url>

</current_observation>

XML Technologies


Below is a list of XML technologies.

XHTML (Extensible HTML)
A stricter and cleaner XML based version of HTML.

XML DOM (XML Document Object Model)
A standard document model for accessing and manipulating XML.

XSL (Extensible Style Sheet Language) XSL consists of three parts:
  • XSLT (XSL Transform) - transforms XML into other formats, like HTML
  • XSL-FO (XSL Formatting Objects)- for formatting XML to screen, paper, etc
  • XPath - a language for navigating XML documents

XQuery (XML Query Language)
An XML based language for querying XML data.

DTD (Document Type Definition)
A standard for defining the legal elements in an XML document.

XSD (XML Schema)
An XML-based alternative to DTD.

XLink (XML Linking Language)
A language for creating hyperlinks in XML documents.

XPointer (XML Pointer Language)
Allows the XLink hyperlinks to point to more specific parts in the XML document.

SOAP (Simple Object Access Protocol)
An XML-based protocol to let applications exchange information over HTTP.

WSDL (Web Services Description Language)
An XML-based language for describing web services.

RDF (Resource Description Framework)
An XML-based language for describing web resources.

RSS (Really Simple Syndication)
A format for syndicating news and the content of news-like sites.

SVG (Scalable Vector Graphics)
Defines graphics in XML format.