The correct way to load an XML document is as follows, IMO:
* Open it
* Run it through the schema to validate it's
structure
* Manipulate/access it via XPath, with the ability to
assume correct structure as defined by the schema.
Compare to JSON, where the process is * Open it
* Attempt to access it, even though it's structure
may deviate from the expected
* Die or try to rollback work when an assumption about
the data structure isn't met.
Thus, most JSON code either trusts the input data implicitly, or is a mess of "is this where it should be?"/independently re-developed schema equivalent every time. I am aware of attempts to create JSON schema languages - unfortunately these are neither in common use, nor well defined enough to be standards, and they're more verbose and less expressive than XML's RelaxNG compact syntax.JSON is fundamentally a dump of a data structure, not a document format. It isn't designed for long term data storage, or for inter-compatibility, despite being used those ways. Thus, it's great if you control both ends and your data structures don't change, as is the case with most web programming. It frequently falls down outside that realm.
Now, the above ignores runtime performance concerns. I'd argue (again) that outside of web apps, the load/store into a data structure is done rarely, or can be done in the background or in parallel if there's a lot of data concerned. Also, we're probably talking about less than an order of magnitude in the vast majority of cases.
The advantage of a schema is that one schema can be used in multiple languages. Someone's Lua based embedded system can run the same schema as a Go program on a 32 CPU box.
You can give someone a well written schema, tell them "make your data like this, and run the validator when you open and save your XML". In this way, it's a free, one line (in most languages) structure sanity check.
In a typed language the very first stage of deserialization, the "turn this from a string into objects in my language" step, will do that for you "for free".
In an untyped language I guess it could be helpful, but again: it's almost always easier to write these sort of checks in a general purpose programming language than in XML schema.
>The advantage of a schema is that one schema can be used in multiple languages. Someone's Lua based embedded system can run the same schema as a Go program on a 32 CPU box.
If that's what you want there are more robust and performant approaches, e.g. thrift.
>You can give someone a well written schema, tell them "make your data like this, and run the validator when you open and save your XML". In this way, it's a free, one line (in most languages) structure sanity check.
Right, but that doesn't tell them what they think it means. It doesn't make the request valid.
> JSON is fundamentally a dump of a data structure, not a document format.
Exactly! IMHO, it's unfortunate that mark-up documents can be used to represent other data structures. It isn't what they're meant for, and there are (or "should be" if you're not fond of JSON) better tools for human-readable serialization.
SAX style streaming/pull parsers tend to deal better with this kind of thing as well.
The original name (Yet Another Markup Language) threw me off, so I never seriously considered using it. Even its Wikipedia article is in the "Markup languages" category.
Yaml for configuration files/things people touch
XML for documents that are complex/nested, that need extra validation, or need to be stored for longer than a few days
JSON for ephemeral data structures passed between two controlled endpoints
It took years and lots of committee meetings for a W3 blessed XML schema language; and then, key people didn't agree with how it should be done: James Clark's alternative was TREX and MURATA Makoto's was RELAX.
Every significant project I've worked on has the same cautious loading process for XML as JSON, with some extra checking at the early XML load stage because XML is harder to work with and thus fewer people produce valid documents (forget schema validations, errors with simple character encoding, well-formedness or namespace declarations are surprisingly common). In practice, I tend to end up with a forgiving parser, a collection of selectors and full validation on the results, which works equally well with either format.
This is, and always has been, an absolutely terrible way of writing off XML. Do we judge every technology today based upon their purported original purpose? "The computer was designed to solve linear equations, therefore it is ill suited for any other purpose". Noisy nonsense.
JSON only maps to the information model of JavaScript, and has no closer ties to any other language than XML does.
By contrast, XML does not directly map to common data structures.
If you believe that XML's information model maps onto "any other language" just as well as JSON, please show me native mapping/object support in XML and, conversely which programming languages (besides XSLT) have mixed content and attributes.
You can serialize to/from XML in virtually any modern language. Not sure if you're seriously asking this, or what the limiter "native" is supposed to mean (beyond that JSON is JavaScript. But it isn't C#, and it isn't Python, and it isn't...).
How they use attributes or elements is context specific. Every feature and pattern doesn't apply to every use.
XML specification is ten times longer; it has dozens of sharp edges you have to memorize in order to use effectively.
No, you actually don't. You create an XSD of a simple layout (trivial, monkey work). That is what you serialize from/to. The universe of possibilities in XML is, again, utterly and absolutely irrelevant.
Seriously, JSON doesn't even have a DATE type. That is so fundamentally broken I don't even know where to start (Microsoft has their own mystical blend of date when they use JSON, for instance, incompatible with anything else).
XML is a poor choice for most serialization since it has the wrong information model. It's designed for extensible document markup and most application data packages aren’t documents that require markup.