<foo>
bar
</foo>
<nested>
<nested_foo>
nested_bar
</nested_foo>
</nested>
Or should I do this?
<foo>
bar
</foo>
<nested nested_foo="nested_bar"/>
If your goal is to simply encode a data structure that looks more or less like a C-style struct (key/values, arrays, primitives), the concept of attributes on tags is superfluous, and introduces ambiguity in how to serialize something.
To me, JSON is nice because it's essentially the minimum viable way of encoding a C-style struct (floating point behavior notwithstanding.) XML has extras like attributes, schemas, and DTDs, which may be useful, but they come at the cost of having additional syntax (<!DOCTYPE ...>, etc), which is auxiliary to the goal of encoding data structures, and thus makes it no longer as minimal.
To me, JSON's approach of having a separate out-of-band definitions of schema (e.g. JSON schema) is the better approach because it's less opinionated, you only pay for it when you need it, and it doesn't require separate syntax. Leave validation to a validation step, my data is my data.
A shitty example (you'd define types with schema), but you get the point.
The data is in the tags, attributes are metadata, extra information that's needed during the process.
This is what JSON is lacking, you can't give metadata to data, like defining types. The type definition needs to live NEXT to the data in special keys you need to check every time when parsing.
I work with an API that returns XML that uses zero tag attributes. It’s all just tags, nested. It’s actually a joy to work with.
I see some XML from other sources and it’s just awful. Everything that should be a nested tag is all jammed into an unreadable tag with attributes with no visual structure at all, spilling over the 80th column and word-wrapping in the terminal, it’s just like, what the hell. Why even use XML at that point?
If I have a structure like this JSON:
Should I do this in XML? Or should I do this? If your goal is to simply encode a data structure that looks more or less like a C-style struct (key/values, arrays, primitives), the concept of attributes on tags is superfluous, and introduces ambiguity in how to serialize something.To me, JSON is nice because it's essentially the minimum viable way of encoding a C-style struct (floating point behavior notwithstanding.) XML has extras like attributes, schemas, and DTDs, which may be useful, but they come at the cost of having additional syntax (<!DOCTYPE ...>, etc), which is auxiliary to the goal of encoding data structures, and thus makes it no longer as minimal.
To me, JSON's approach of having a separate out-of-band definitions of schema (e.g. JSON schema) is the better approach because it's less opinionated, you only pay for it when you need it, and it doesn't require separate syntax. Leave validation to a validation step, my data is my data.