by cryptos · 3y ago · 0 comments
XML is considered overly verbose, which is not always the fault of XML, but of how it is used.
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.2</version>
</dependency>
Could (in principle) be written as: <dependency groupId="org.junit.jupiter" artifactId="junit-jupiter-engine" version="5.9.2" />
What really stands out is XML Schema. It is so much more powerful and precise compared to Json Schema.See also: http://www.nichesoftware.co.nz/2017/04/25/in-defense-of-xml....
Your example is simple enough, but consider the following:
<root>
This is some text.
<type1 attributes="yes">
This is content with <i>markup</i>.
</type1>
<type1>
Another element of the same type.
</type1>
<type2>
And this is an element of an entirely different type.
</type2>
This is more text.
</root>
How the hell can I map that to my data model? My application data will, at worst, be a graph of class instances with named fields, containers (lists, sets, etc), and primitives (strings, ints, etc).The fact that XML has both attributes and subelements is already a huge impedance mismatch because classes have a single namespace. But what do I do with the repeated element? With the text interspaced with tags? What's the API to navigate this mess?
I'm pretty sure that's why JSON won: it started from the application data model and just simplified it (no custom types, trees only). A deserialized JSON object doesn't even need an API, it's just a nested structure of built-in types found in any modern programming language.
You ask about API: XML mapping problem has been solved more than 20 years ago and standard cross-platform API exists since then. Have you ever heard of DOM? Have you ever heard of data mappers? XML Schema-based code generation? In modern Java you can even use the same mapping both for JSON and XML.
There are two reasons why JSON dominates in certain applications: easier to write a small structure, browser-friendly. XML still has advantages in large datasets and schema validation.
JSON can be deserialzed to hashmaps / lists with a single call that has no schema or parsing instructions. The spec unambiguously deserializes a "data document" to a data structure.
XML cannot do this:
- possibility of text being just ... somewhere ... it wasn't intended.
- tag data vs attribute data: ambiguous
- a list CAN be represented in XML, but there is no array begin/end indicator like JSON that will confirm that the tags are a list, versus some other screwy thing
- finally, JSON can represent a "data document" that is just an array/list, while XML cannot do this unambiguously. It has to have a root tag.
As for schema validation, that was always a hack. Schema will ultimately encounter domain-impacted information, like "is this a valid ID in our database" that a schema rule can't encode.
Because JSON is so readily deserialized to, at a minimum, hashmaps and lists, it can be readily validated with code. Virtually all JSON deserialization libraries also provide the ability (in typed languages) to provide a desired type to deserialize into, and the type information of the member variables, even lists/maps, is decorated such that those can be deserialized as well.
In that case, the JSON deserialization-to-type provides an automatic schema validator with no schema rules to maintain alongside the class specification.
If you REALLY like XML because it kind of self-documents a bit better or stuctures better or ... well I can't really think of a reason why to use XML, but anyway I recommend YAML as a modern replacement.
God I wish XPath was ported to json/yaml/toml land though. That was the bees knees as my grandma used to say.
You don‘t have that with other formats.
And clients just map whatever defined in schema.
In the case above, not so much (though I'd say it's the artifactId)
But since XMLers like waddling in verbosity and bureaucracy it seems they shot their own foot with it
As someone who was involved in making XML based international standards (HR-XML), I think what made JSON great was that it was so simple that there was initially no industry to be made out of making schema. XML had so many ways to do things a schema was needed. JSON? Not so much.
XML shall be used for Good, not Evil.
9 times out of 10 for data transport you want SAX parsing not DOM. Unless your XML is actually a marked up document DOM is overkill. But many data transport libs used to use DOM which has a very real performance cost. I'm not sure how much of that is still true but the reputation for bloat and being heavy weight was often caused by parsing to DOM and then deserializing from there.
Read the entire document into memory and let Jackson parse it out into a heavyweight object graph. Blech.
That's not the standard's fault. DOM and SAX (or rather streaming parsing in general) both have their places, it's up to the developer to know when to use which.
Of course, developers often make bad choices, but the only way a standard can "solve" that is by forcing a developer to make bad choices in some contexts, so it's not the developer's fault anymore.
I wished many times if I could just use an arena with a dumb allocator for a small document and just drop it when I finish with the document, but it's not very easy to do with libxml2, unless you write your own DOM parser on top of SAX.
Stream parsing is indeed preferable when you can get away with it.
Maybe so, but the continuing popularity of JSON, YAML, TOML, and recent competitors shows that developers (in general) prefer something that is both easily readable and editable by hand.
Personal opinion of course, but I wouldn't call XML precise, I would call it the opposite because it's too flexible. The older I get, the more I think programming languages/markup languages/config languages should be opinionated and strict.
Ahh, "MicroISV". That is a term I don't hear enough these days!(Greetings from a UK based micro MicroISV)