Why and when to use XML instead of JSON?

16

JSON has at least two advantages over XML:

  • parsing is faster
  • Occupy less bytes
  • There are still people who prefer XML. Is there any reason to prefer XML? Or in what situations is it most appropriate (and why)?

        
    asked by anonymous 06.06.2014 / 17:58

    4 answers

    15

    The decision whether to use XML or JSON is more or less like choosing between a relational database or a NoSQL: it all comes down to deciding on an explicit or implicit < strong>.

    A schema ) is a description of a dataset (i.e. metadata). It determines the type of data, its format, the relationship between them, how they are structured, and so on. If these data are for human consumption, the scheme does not matter as much - since we have the ability to read and interpret the content ahead of us even though it is malformed and "messy." But if the data is for machine consumption (i.e. it will be read and handled by other programs) then a formal scheme will always be needed - whether it is meta-data encoded or just convened.

    As in a relational DB you need to create tables, define their columns, their keys and constraints, etc. An XML file can contain the same type of description (in the form of a DTD or Schema) - saying that elements are accepted, what attributes and sub-elements they can have, etc. In this way, it is possible to automate the process of checking whether or not an XML data is valid according to its specification. That is, just as you can not assign a column that does not exist in a DB, you can not create an element that does not exist in the schema and the XML is still valid.

    On the other hand, it is possible to create XML without a schema - where all content is valid as long as the syntax is correct. It would therefore be the responsibility of the programmer to ensure that this content is generated and consumed according to a logical rule. If it is the same programmer doing both sides, it is easy, because when changing one he will know that he has to change the other (can not consume what does not exist, or produce something for anyone). But if it's a large team - or worse, a set of teams or even different organizations - it's harder to control the changes and make sure all the sub-systems involved deal with them correctly.

    However, if for the particular characteristics of a situation an explicit schema is not even necessary, why not yet use XML? In this case, the disadvantages of XML make it a less than ideal solution - syntax is verbose (all open tags have to be closed), files become large and therefore difficult to read and write, and XML processing is more difficult and slow. In this case, a lighter solution is preferable - in the same way a BD schema-less will be simpler to use and will perform better than a relational DB trying to handle arbitrary, .

    JSON proved to be an appropriate solution for such cases. It is not the only, nor necessarily the best one (on Wikipedia there is a comparative among several data serialization formats ) but as often the "client" side of the communication is a browser - and so it supports natively JavaScript - this additional closeness between the format and literals of the language itself has the added benefit of greatly simplifying the client code *. I think this was a decisive factor in consolidating JSON as the preferred "light" alternative to XML. And since it came to receive broad support on most platforms, its ubiquity further increased its advantage.

    * I say this in historical terms: formerly it used the eval of JavaScript itself to interpret JSON, but after the XSS threat became evident and well known, it started to use its own functions to do the JS-JSON conversion.

        
    06.06.2014 / 20:59
    9

    JSON:

    The JSON object is sometimes used as a "communication bridge" because it is lightweight and fast, different from XML that has a sharper structure. The way the JSON object is manipulated is also one of the organizing facilities, making the code cleaner and dedicated only to the data structure. Here's an example in ActionScript:

    var json:Object = {nome: "João", idade: 18, veiculos: ["Twister", "BMW"], parentes: {mae: "Maria", pai: "Joaquim"}};
    

    Not to mention that it is possible to insert an Array directly into an item, just like another JSON object.

    This ease of handling along with speed / lightness is one of the main reasons that makes JSON the perfect tool for data communication with the server.

    XML:

    I believe that XML can work as a "mini-bd", recording some crucial information for the development of some system, be it local or web.

    The data management system is a bit more structured, where you can add parameters and values in a certain item, as below:

    var xml:XML = new XML(
        <root>
            <atualizacao data="06/06/2014"></atualizacao>
            <nome>João</nome>
            <idade>18</idade>
            <veiculos>
                <veiculo>Twister</veiculo>
                <veiculo>BMW</veiculo>
            </veiculos>
            <parentes>
                <pai>Joaquim</pai>
                <mae>Maria<mae>
            <parentes>
    
        </root>
    );
    

    Although the structure of an XML is organized and well understood, the interaction with the Web can leave a little more to be desired, because it is a little more complicated to work to rescue all this information, taking the fact that you can not join an Array object inside a node, creating several nodes with identical name.

    How do I retrieve these two information:

    trace(json["nome"]); //"João"
    trace(xml.nome); //"João"
    
    trace(json["veiculos"]); //[Twister, BMW]
    
    trace(xml.veiculos); //<veiculo>Twister</veiculo><veiculo>BMW</veiculo>
    trace(xml.veiculos.veiculo[0]); //Twister
    trace(xml.veiculos.veiculo[1]); //BMW
    
    trace(xml.atualizacao.@data); //06/06/2014
    

    Yes, there are these differences between them that can influence the final outcome of your system.

        
    06.06.2014 / 18:06
    5

    XML has many features and uses that JSON does not support.

    You can search for it through XPath , XQuery and you can validate Schema and many other things .

    Is there room for the two technologies, the XML being more robust (and bureaucratic) and JSON lighter (and less controllable?)

        
    06.06.2014 / 18:06
    3

    It depends on the context, my dear. If you are considering choosing between one of them to expose results of an API, for example, it will depend on the standard of your API. Typically, Restful APIs tend to use JSON, whereas web services in the WSDL standard use XML.

    XML, by following a markup pattern, will allow you more flexibility, while JSON is a simpler, more straightforward standard - and is now more widely accepted in the web context, especially when it comes to single-page applications .

        
    06.06.2014 / 18:03