JSON vs XML
Types of Web Services:
Comparison of JSON and XML
Both JSON and XML can be used to receive data from a web server.
What is JSON?
JSON stands for?JavaScript?Object?Notation. It is a text format?for storing and transporting data. JSON is Easy to use; JSON API offers a high-level facade, which helps you to simplify commonly used use cases. JSON library is open source and free to use. JSON is faster as it consumes very less memory space.
{"employees":
??{?"firstName":"John",?"lastName":"Doe"?},
??{?"firstName":"Anna",?"lastName":"Smith"?},
??{?"firstName":"Peter",?"lastName":"Jones"?}
]}[
What is XML?
XML stands for Extensible Markup Language. XML is a meta-language that allows you to create and use tag sets in a standard way. XML is not a set of tags, we need to define your customized tags and easy to understand for a human. XML is an extensible markup language like HTML.
<employees
??<employee>
????<firstName>John</firstName>?
<lastName>Doe</lastName>
??</employee>
??<employee>
????<firstName>Anna</firstName>?
<lastName>Smith</lastName>
??</employee>
??<employee>
????<firstName>Peter</firstName>
?<lastName>Jones</lastName>
??</employee>
</employees>>
Similarities:
领英推荐
Differences:
How AJAX Works in Both JSON and XML:
The XML DOM views an XML document as a tree structure. The tree structure is called a node tree.?The nodes can be accessed with JavaScript or other programming languages. The programming interface to the DOM is defined by a set of standard properties and methods.
function myXMLFunction(xml)
var i;
var xmlDoc = xml.responseXML;
var table="<tr><th>Id</th>“+
"<th>firstname</th>“+
"<th>lastname</th></tr>";
var x = xmlDoc
.getElementsByTagName("employee");
for (i = 0; i <x.length; i++) {?
table += "<tr><td>" +?x[i].getElementsByTagName("id")[0].childNodes[0].nodeValue +
"</td><td>" +x[i].getElementsByTagName("firstName")[0]childNodes[0].nodeValue +
"</td><td>" +x[i].getElementsByTagName("lastName")[0].childNodes[0].nodeValue +?
"</td></tr>";
}
document.getElementById("result").innerHTML = table;
}
The data is always a string when receiving data from a web server. Parse the data with JSON.parse(), and the data becomes a JavaScript object. JavaScript has a built-in function for converting an object into a JSON string: JSON.stringify()
function myJSONFunction(json)
var i;
var jsonDoc = json.response;?
var table="<tr><th>Id</th>"+ "<th>firstname</th>"+"<th>lastname</th></tr>";
var x = JSON.parse(jsonDoc);
for (i = 0; i <x.length; i++) {?
table += "<tr><td>" +
x[i]["id"] + "</td><td>" + x[i]["firstName"] + "</td><td>" + x[i]["lastName"] +
"</td></tr>";
}
document.getElementById("result").innerHTML = table;
}
References:
JSON vs XML. (n.d.). https://www.w3schools.com/js/js_json_xml.asp
Web services and Service-Oriented Architecture: principles and technology, 2nd Edition, Michael P. Papazoglou, Pearson, 2012.?