JSON vs XML

JSON vs XML

Types of Web Services:

  • JAX-WS: Communication using XML, provides for message-oriented and RPC services, uses SOAP messages,?and includes standards for security and reliability.
  • JAX-RS: Standard and semantics of the data to be exchanged are understood?by the client and server.

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:

  1. Both are human-readable
  2. Both have very simple syntax
  3. Both are hierarchical?
  4. Both are language independent
  5. Both can be used by Ajax

Differences:

  1. Syntax is different
  2. JSON is less verbose
  3. JSON includes arrays
  4. Names in JSON must not be JavaScript-reserved words.
  5. XML can be validated.
  6. JavaScript is not typically used on the server side

How AJAX Works in Both JSON and XML:

  • 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;
}        

  • JSON

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.?

要查看或添加评论,请登录

Qabas Al-Mamari的更多文章

  • Absolute Positioning

    Absolute Positioning

    In CSS there are a couple of different positioning modes, but the more important ones we will talk about for now are…

    3 条评论
  • JWT - Token Authentication

    JWT - Token Authentication

    Suppose we request our API and it should return data. Tokens are a good thing to use with an API because they are small…

  • Storing Passwords in the Database

    Storing Passwords in the Database

    This article looks at how we can store something like a password in a database. Now it goes without saying that the…

    1 条评论
  • What is BPMN?

    What is BPMN?

    The main benefit of BPMN is that allows you to create and share diagrams that business stakeholders will readily…

社区洞察

其他会员也浏览了