Here's a comparison for JSON and XML
1. When you do a request data from server, on the client side you do:
a. JSON:
var data = eval('(' +ajax.responseText+ ')'); or
var data = null; eval('data ='+ajax.responseText); or
var data = new Function('return '+ajax.responseText);
b. XML:
var data = ajax.reponseXML;
2. On the server side, the process for the request handling is:
a. JSON:
$data_json = json_encode($data_array);
b. XML:
$data = '<?xml version="1.0"?>';
header('Content-type: text/xml');
echo $data;
3. When you do sending data to server, the process in the client is like this one:
a. JSON:
var data = {};
var data_sending = JSON.stringify($data);
b. XML:
var xml_data = '<?xml version=\"1.0\"?>';
var parser = new DOMParser();
var xmlDoc = parser.parseFromString(xml_data, 'text/xml');
4. And the process on the server is like:
a. JSON:
$data = $_GET['data'];
$data_array = json_decode($data, true);
b. XML:
$data = $_GET['data'];
$xmlDoc = new DOMDocument();
$xmlDoc->loadXML(data); // or load(file);
$xmlDoc->loadXML(data); // or load(file);
$names = $xmlDoc->getElementsByTagName('name');
$name = $names->item(0)->nodeValue;
5. Other similarities:
a. Converting native object to string representations:
- JSON => JSON.stringify(data);
- XML => ?????????
b. Converting string text to native object:
- JSON => var data = text.parseJSON();
- XML => var doc = new DOMParser();
var xmlDoc = doc.parserFromString(data, 'text/xml');
No comments:
Post a Comment