JSON or XML, which format to choose
?With this comparison of JSON and XML, we will try, highlighting the strengths and weaknesses of each format, to help you choose the one that is best suited for a particular application, and especially for the Ajax/HTML 5 application.
JSON
JSON is a recursive format compatible with JavaScript that makes it the structure of a JavaScript object. This is the object saved in the file. It dates from 2002 and suddenly became popular when Ajax became widely used. JSON files can be stored in the PostgreSQL database, there is a W3C standard for representing form data in JSON, among other applications.
JSON assumes you know the structure of the document. When used with any programming language, data is accessed by object structure.
1) JSON и JavaScript
JSON is very easy to use in JavaScript, it is part of the language.
To use a JSON file, you just need to load the file as text, this is the content of the Ajax responseText attribute.
You then use the JavaScript eval () function to create a JavaScript object:
var doc = xhr.responseText;
var jdoc = eval('(' + doc + ')');
or with a new browser:
var jdoc = JSON.parse(doc);
After parsing the file, it is used like any JavaScript object:
var value = jdoc.commands[0].value;
var action = jdoc.commands[0].action;
Many scripts are available on the web to serialize a JavaScript object into a JSON file. Using a recent browser, the object is converted to a string :
var str = JSON.stringify(jdoc);
2) JSON и PHP
You can use JSON in PHP after parsing a file with a PHP parser. There is a JSON library for PHP, it is enough to configure php.ini to use it. There is also a json.php library that must be enabled directly with the "require" function.
3) Web Service
The JSON Web Service Library framework is very popular.
4) Transformations
Conversion from one format to another can be done by serializing the object into memory in the new format.
5) Database
JSON is a data type for PostgreSQL .
6) Example JSON file
[ {
"menu": "File",
"commands": [
{
"value": "New",
"action":"CreateDoc"
},
{
"value": "Open",
"action": "OpenDoc"
},
{
"value": "Close",
"action": "CloseDoc"
} ]
} ]
XML
XML is a tag language that is the basis of many interface formats and languages: RSS, SVG, OPML, XHTML, Open XML, XAML, etc. It allows you to describe and analyze all types of documents, except binary ones, and store them in a file.
This is more verbose than JSON, but there are a number of tools for processing it, and it is also a text processing file format and other office software.
XML structure is free. Any text content represents itself in XML and can be accessed by tag ID or tag name. But the format takes up a lot of space.
Using DOM methods can be time-consuming with XML and slow with XHTML.
1) XML и JavaScript
Unlike JSON, the file is loaded into Ajax directly as an XML document, this is the responseXML Ajax attribute.
Content can then be accessed using DOM methods.
var xdoc = xhr.responseXML;
var x = xdoc.getElementById("mabalise");
2) XML и PHP
XML is part of the main PHP 5 language, it is used there directly with the DOMDocument and SimpleXML classes. You can load an XML file, process the content using DOM methods, and save it directly to a file.
3) Web Service
There is an XML format for web services: SOAP. This is the W3C standard, but quite complex and not very popular.
4) Transformations
Converting an XML document to another format can be done by XSLT, a tool that is not very easy to use. Of course, you can also load an XML file in PHP or JavaScript, take data and assemble the file in a new format, sometimes it is easier.
5) Database
The combination of XML and XPath allows it to be used as a database, so it is suitable for large resources.
6) Example XML file
<?xml version="1.0" ?>
<menubar>
<menu name="File">
<command value="New" action="CreateDoc" />
<command value="Open" action="OpenDoc" />
<command value="Close" action="CloseDoc" />
</menu>
</menubar>
There is great freedom in how to present data:
<command>
<value>New</value>
<action>CreateDoc</action>
</command>
Conclusion
JSON is easier to delete data on the server, and use it as permanent memory for the program. To use it, you need to know the data structure, it is advisable to be the owner of the file. It is lighter than XML and saves resources.
XML is better suited for presentation. This is the language of many graphical interfaces now: XAML, XUL, MXLM, etc., while QML is close to JSON. In the first case, the data is stored in one form and used in another.
You can use XML from external sources and even create XML databases.