Why would anyone choose to use XML over JSON, other than for RSS?
I can parse/print XML (using either in memory parser or streaming parser), use XML Schema to validate XML, XPath expressions to select necessary parts, automatic object mapping, and that's all with standard library without a single external dependency in Java. I don't know why would I use JSON over XML, unless I have very good reasons to do so. For me the only thing that JSON got better is that JSON is directly mappe…
This is nice, but it's also kind of a pain, as it makes you have to stop and think about which structured data elements it's capable of supporting and which you have to send your own metadata through the wire and then reconstruct on your own. For example: Dates. Which is a shame. If there is one data element I want the most help with serializing/deserializing, it's freaking Dates. All the other ones are super easy in comparison. There's just way to much subjective, dirty, human culture tied up in Dates.
The only thing that I think is objectively better in all cases about JSON over XML is the less verbose end-structure syntax. I think XML only has Attributes because Tags have this silly need to state their name both as they enter and exit the room.
how
are
you?
For simple, unnestable data elements, having the more efficient Attribute starts to look attractive.If XML were more the form:
how
are
you?
It's actually only one more additional, required character to add than if the attribute value were specified as an element instead.
hello
world
how
are
you?
Heck, why stop there? Do we really need to have quite all of those angle brackets now? How about we just get rid of all the ones we can assume:
>
And finally, who even likes angle brackets? I've never enjoyed the dual duty they play as delimiters in XML and operators in other languages. Let's use a common set delimiter, something like square brackets or maybe parentheses. (Tag1
(attr1 hello)
(attr2 world)
(Tag2 how)
(Tag2 are)
(Tag2 you?))
Now where have I seen this before?PS: JSON that is as nearly as equivalent as I can make it is not much less verbose than original XML, and requires some level of convention to make up for the differences:
{Tag1: { attr1: "hello", attr2: "world", children: [
{Tag2: "how"},
{Tag2: "are"},
{Tag2: "you?}]}
Though I'm sure in common practice it'd have a lot of the original metadata of the XML version thrown away: {attr1: "hello", attr2: "world", children: [
"how",
"are",
"you?"]}