Magazine
 
AJAX: Redefining Web Applications
 

/Using XML
Entered key: 12
<converted-values>
<binary>1100</binary>
<octal>14</octal>
<decimal>12</decimal>
<hexadecimal>c</hexadecimal>
</converted-values>

If you do not provide any value, it behaves similar as shown below:

When the number 67 is entered, it behaves as:

Corresponding output at the server console:

Using XML
Entered key: 67
<converted-values>
<binary>1000011</binary>
<octal>103</octal>
<decimal>67</decimal>
<hexadecimal>43</hexadecimal>
</converted-values>

 

In the above servlet, we used simple StringBuffer class to create a xml as a response string. This response string is then parsed at the client-side by leveraging the built-in xml parsers in the browser. There are two different

  functions given in the html file, i.e. msPopulate(
) for IE browser and nonMSPopulate( ) for non-IE browsers. The extracted data is then placed into the different UI elements of the web page.

II. Constructing and parsing the response data with JSON:

• One major drawback with XML is speed.
• XML requires two tags per data point, plus extra tags for parent nodes. All this extra data in transmission slows down the data exchange between the client and server.
• It means that the data transmission becomes slower as the response XML string becomes bulkier.
• So, there is another way to send data to the client that is easier to parse and more compact. That alternative is JSON
[JavaScript Object Notation].

• Advantages of using JSON:

• JSON objects are typically smaller than the equivalent XML documents.
• Working with them is more memoryefficient.
• You can parse JSON with JavaScript’s eval() function – For this you don’t need other libraries, and you don’t need to worry as much about cross-browser functionality.
• As long as your browser has JavaScript enabled and supports the eval() function, you will be able to interpret the data.
• Following is the data object represented in JSON:

{
“conversion”:
{
“decimal”: “120”,
“hexadecimal”: “78”,
“octal”: “170”,
“binary”: “1111000B”



Nov 2007 | Java Jazz Up | 53
previous
index
next
 
View All Topics
All Pages of this Issue
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

58
, 59, 60, 61, 62, 63 , 64, 65 , 66 , 67 , 68 , 69   Download PDF