Harvard Extension School
CSCI E-12 Fundamentals of Web Site Development | Fall 2019

## XML - eXtensible Markup Language

XML gives a way of defining other markup languages (for example, HTML is a markup language that has an XML format). The "X" is for extensible, so XML is flexible in the data and documents that it can contain.

> There is essentially no computer in the world, desk-top, hand-held, or back-room, that doesn't process XML sometimes. This is a good thing, because it shows that information can be packaged and transmitted and used in a way that's independent of the kinds of computer and software that are involved. XML won't be the last neutral information-wrapping system; but as the first, it's done very well.
> Tim Bray (Sun Microsystems)

> Quote from the press release of the W3C XML 10 Celebration in 2008

An example of course data in XML format is:

When working with XML in JavaScript, you can use the DOM, or there are libraries to convert XML to JSON.

One important aspect of XML is that it is really a family of technologies that interoperate quite well -- once you are in the XML world and using XML technologies, working with XML can be easy, straightforward, and even fun. Examples of XML technolgoies are:

- Programming models: DOM and SAX
- Query: XQuery and XPath
- Transformation: XSLT
- Presentation format: Office document formats are XML based, SVG (graphics), XSL-FO (print), XHTML/HTML (web)

So an example where we have the seasons array in XML:

    <seasons>
      <season>Spring</season>
      <season>Summer</season>
      <season>Autumn</season>
      <season>Winter</season>
    </seasons>

We could process that in jQuery by:

    $.get("seasons.xml",function(data){
      var ul_node = $('<ul/>');
      console.log(data);
      $(data).find("season").each(function(){
        var season_string = $(this).text();
        console.log($(this));
        $('<li/>').text(season_string).appendTo(ul_node);
      });
      ul_node.replaceAll($('#seasonslist'));
    });

Full code:

Working Examples:

- Live Example - Seasons in XML
- Seasons XML in JS Fiddle

Which is better, JSON or XML ? There are quite a few strong opinions as to whether JSON is better than XML or XML is better than JSON. The answer, of course, is "it depends". For delivering data to a web browser to process with JavaScript and present on a web page, JSON comes out ahead of XML. However, there are many cases where XML will be judged the winner over JSON.

Copyright © David Heitmeyer