California Ajax Solutions Team

www.CalAST.com

The X in Ajax comes from XML, which itself stands for eXtensible Markup Language. XML is a language for describing structured data, and is ideal for transmitting lists of things - such as the results of a database query. It is written in text, rather than binary. Thus, it is easily read by humans (or browsers) and it can be read programmatically. Since XML is structurally similar to HTML, it is possible to manipulate an XML document using the same functions that are used to work with HTML. XML was written in 1996 and approved as a standard in 1998, so it has been around a while.

In general, XML consists of elements, which are marked with starting and ending tags that look pretty much the same as the tags in HTML. The primary differences is that the tags in HTML are standardized so that any HTML document can be read by any browser and rendered in more or less the same way. The HTML table element always produces a table, for example. In XML, the designer specifies the name of the element and its meaning. If you are describing a person, you might have elements for name, address, telephone, email, and so on. Since only the applications that produce and consume a given set of XML elements have to agree on the names, you are free to choose whatever seems appropriate.

The tags for XML look pretty much like HTML tags, and may enclose text between the start and end element tags in the same way. There can be attributes associated with XML as well. The document itself is represented as a tree structure. A valid XML document must have exactly one root node from which everything is derived. Children of the root node must be properly nested, and may not overlap. Text containing special characters needs to be escaped in much the same was as in HTML. For example, the '<' character must be specified as &lt; to distinguish it from the start of a tag.

In the example at right, we have a tree representing a football team, in this case the New England Patriots. The team name is used as the text component of the team element. The team node has child nodes describing its conference and division. There is also a roster child node. The roster node itself has child nodes for each position (we only show one position and two players) with the position name as text. Each player at that position will have its own node, with the player node containing an attribute indicating if this player is a starter or not. The player node has two child nodes, number and name.

Despite the extensible nature of XML - that is, the user-defined element names - it is often recommended that XML documents be validated. To ensure that any newly created document can be read by an existing application, a DTD (Document Type Descriptor) may be used. This specifies the names of all the tags and attributes and their structural relationship. If the consuming application is properly written to conform to the DTD, then any DTD-compliant document should work with it. Instead of behaving in unpredictable ways in response to an improperly
formed input, the document is simply rejected as being non-conforming. This is clearly safer.

While XML is widely used, there are some alternatives for passing structured data between server and client. In Ajax, JSON (JavaScript Object Notation) is sometimes used instead, as it is well-suited to forming objects that can be directly manipulated by JavaScript on the client side. It is also somewhat more 'lightweight' in that it does not require the textual overhead of starting and opening tags.
XML

<team>New England Patriots
  <conf>AFC</conf>
  <division>East</division>
  <roster>
   <position>Quarterback
    <player starter="yes">
     <number>12</number>
     <name>Brady, Tom</name>
    </player>
    <player starter="no">
     <number>8</number>
     <name>Hoyer, Brian</name>
    </player>
  </position>
 </roster>
</team>