The following are some examples of these different formats:
- HTML content
- Plain text or string format
- XML (Extensible Markup Language)
- JSON (JavaScript Object Notation)
One of the more common data interchange formats is the normal HTML content being transferred from server to client. When the server sends the HTML content as a response to a request, this content can be accessed by JavaScript and can be placed in any element using the innerHTML property or related methods. In our example of the Items list, the HTML content that would be sent to the browser is as follows:
<div><span>Computer .Net Course</span><span>Advance</span>
<span>2007</span><span>$ 896.00</span></div>
<div><span>Computer C# Course</span><span>Premier</span>
<span>2006</span><span>$ 655.00</span></div>
<div><span> Computer ASP.Net Course </span><span>Regular</span>
<span>2007</span><span>$ 720.00</span></div>
This content can be taken as is and inserted as HTML content in any of the elements that have the innerHTML property. Therefore, it is simple to bind, but formatting the HTML received from the server would be a big disadvantage.
The Most Common Plain Text or String Format :
Plain text or string format is a simple data interchange format as it does not involve any complexity. The server just returns plain text, and this can be bound to any element in the HTML using properties like value, text, and innerText based on the element chosen for display. We can also go for delimited string values as the response format for related data, although this has some disadvantages. Some of the ASP.NET Ajax server controls we examine later in the book emit delimited string values in their response format. One such control is an UpdatePanel control, which emits the response in the following format:
Size|ControlType|ControlName|ControlData The preceding string is delimited by “|” (the pipe symbol). Here, Size is in number of bytes, ControlType is the type of control (in this case, it is UpdatePanel), and ControlName is the name assigned to the control. The last delimited value is ControlData, which holds HTML content to be displayed on the browser. The advantage of using this format is the simplicity in coding, but the disadvantage is that if the position of the values is changed, it requires changes in the client code.
XML (Extensible Markup Language) :
XML is a markup language that was designed to describe data. XML stands for EXtensible Markup Language and is a W3C recommendation. XML today is a common standard for exchanging and manipulating data. It is one of the most commonly used data interchange formats in today’s web- and windows-based applications, because of its wide acceptance among several software vendors.
The exchange of data in XML is more so between the web server and client browser, as most browsers today implement XML DOM specifications, making it easier for the JavaScript to handle XML. XML does not have any predefined tags, and you must define your own tags to describe data. It uses a Document Type Definition (DTD) or an XML Schema to describe the data. In other words, a DTD or an XML Schema performs a grammar check to ensure its validity. This has gained popularity because of the simplicity in its data representation and its support for hierarchical data. Also, XML is free and extensible.
The following is an example of XML data representation:
<?xml version=”1.0” encoding=”utf-8” ?>
<ItemList>
<Item>
<Name>Computer .Net Course</Name>
<Type>Advance</Type>
<Year>2007</Year>
<Fee>$ 896.00</Fee>
</Item>
<Item>
<Name>Computer C# Course</Name>
<Type>Premier</Type>
<Year>2006</Year>
<Fee>$ 655.00</Fee>
</Item>
<Item>
<Name>Computer ASP.Net Course</Name>
<Type>Regular</Type>
<Year>2007</Year>
<Fee>$ 720.00</Fee>
</Item>
</ItemList>
The preceding XML data represents Items and its attributes, such as Name, Type, Year, and Fee. It is a simple structure that is self-explanatory.
The XML data retrieved from the server can be displayed on the browser in a couple of ways, as follows:
Use of XML DOM specification—This API can be used to parse the XML, access it, and transform into HTML content using JavaScript. To know more about the specifications of XML DOM API, go to www.w3.org.
Use of XSLT to transform the document into HTML—XSLT is a declarative, XML-based transformation language used to convert an XML document from one form to other. The XSLT processor uses an XSLT style sheet for converting XML document to HTML, plain text, or any other format supported by the processor.
JSON (JavaScript Object Notation) :
XML has become the defacto standard for data transmission. In Ajax-based applications, XML can be accessed by the XMLHttpRequest object in XML format using the responseXML property or in string format using the responseText property.
However, choosing XML as a data exchange format has its downsides, too. It is not suited especially when heavy data has to be exchanged between the server and client. Why? First, it becomes difficult to parse huge XML and access data; second, the XML data format is higher in bytes in what could have been accomplished in a smaller form.