Magazine
 
AJAX: Redefining Web Applications

Let’s construct and parse the response data:

I. Constructing and parsing response data using XML:

• There is XML parser is built into most browsers; we just have to leverage the built-in parser.
• On the Servlet side, we can construct XML using the following ways:
• StringBuffer [common approach]
• JDOM
• DOM4J
• SAX [Simple API for XML]
• Faster than JDOM & DOM4J
• On the Client side, we’ve to parse the response XML string using the browser inbuilt parser.
• Firefox, Mozilla, Opera, and Safari all use new DOMParser() to get a built-in parser that can parse XML
• Internet Explorer, on the other hand, uses new ActiveXObject(“Microsoft.XMLDOM”) to get the Microsoft XML parser.


• Example XML response string:

<converted-values>
<decimal>97</decimal>
<hexadecimal>0x61</hexadecimal>
<octal>0141</octal>
<binary>1100001B</binary>
</converted-values>

Code illustrations for Constructing and parsing response data using XML:

AJAXCharacterDecoderUsingXML.html

<!DOCTYPE HTML PUBLIC “-//W3C//DTD
HTML 4.01 Transitional//EN”>
<html>
<head>
<title>AJAXCharacterDecoder.html</title>

  <meta http-equiv=”keywords”
content=”keyword1,keyword2,keyword3">
<meta http-equiv=”description” content=”this
is my page”>
<meta http-equiv=”content-type”
content=”text/html; charset=UTF-8">
<!—<link rel=”stylesheet” type=”text/css”
href=”./styles.css”>—>
<script language=”javascript”>
var request;
function convertToXML( ){
var key = document.getElementById(“key”);
var keypressed =
document.getElementById(“keypressed”);
keypressed.value = key.value;
var url = “/ajaxapp/
CharacterDecoderUsingXML?key=” +
escape(key.value);
if(window.XMLHttpRequest){
request = new XMLHttpRequest();
} else{
request = new
ActiveXObject(“Microsoft.XMLHTTP”);
}
request.open(“Get”,url,true);
request.onreadystatechange = callback;
request.send(null);
}
function callback( ){
if(request.readyState == 4){
if(request.status == 200){
if(window.XMLHttpRequest){
nonMSPopulate();
} else{
msPopulate();
}}}}
function nonMSPopulate( ){
var response = request.responseText;
var xmlDoc =
document.implementation.createDocument(“”,””,null);
var parser = new DOMParser();
var dom =
Nov 2007 | Java Jazz Up | 50
 
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