| ▲ | kccqzy 11 hours ago | |||||||||||||||||||||||||||||||
In my opinion, the reason people hate XML is because of what M signifies: it is a markup language and most of the time we don’t need a markup language. Markup languages are great for rich text documents. They are just not a good fit for representing data. The markup-nature of XML introduces unnecessary choice in whether to use an attribute or a child element to represent data; for HTML such ambiguity doesn’t actually exist but for data it does. Consider this piece of XML from the Python docs:
Why is the country name an attribute but not the rank? Why are all information about neighbors attributes but not children?Furthermore parsing JSON or YAML gives you an AST that consists of the basic data types like lists and dictionaries. Parsing XML gives you an AST that requires a lot more effort to turn into data in your domain. Even on the web, very few people like to use the verbose XML DOM API like childNodes, nodeType, getElementsByTagName et al; it is basically unheard of for anyone to use it outside the web such as in Python, despite that the DOM API is in the Python standard library since forever (see https://github.com/python/cpython/blob/3.14/Lib/xml/dom/mini... for example). | ||||||||||||||||||||||||||||||||
| ▲ | HarHarVeryFunny 10 hours ago | parent | next [-] | |||||||||||||||||||||||||||||||
Attributes are intended to hold metadata, not data. It's not the fault of the format if someone chose to use it in a poor way. You also need to distinguish the format itself from the various libraries that may be available to parse/process it in a given language. It doesn't always make sense, even when it's an option, to let the tail wag the dog and choose a language just because it has a nice library for something. > Furthermore parsing JSON or YAML gives you the basic data types like lists and dictionaries Well, maybe some library for some language does that, and if that is the language you are using, and that is all you need, then I suppose you are in luck. More generally you may want to use a format like XML or JSON to hold user defined types, which rather levels the playing field since there are few good libraries for this in any language,and you may need to roll your own (been there, done that). | ||||||||||||||||||||||||||||||||
| ▲ | dgrunwald 11 hours ago | parent | prev | next [-] | |||||||||||||||||||||||||||||||
> Furthermore parsing JSON or YAML gives you the basic data types like lists and dictionaries. Parsing XML gives you an AST that requires a lot more effort to turn into data in your domain. More precisely: in XML, elements (nodes) are named/labeled. ("node-labeled graph") In JSON, keys (edges) are named. ("edge-labeled graph") In programming, we need names for the fields in our structures (edges between objects), so JSON is a much better match than XML (which needs contortions to handle this use case -- e.g. by having nesting levels alternate between element=node and element=edge). Only in some object-oriented cases (which derived class should the deserializer construct?) do you care about node labels -- but usually that's in addition to edge labels, so a "_type" key in JSON is still easier than XML. | ||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||
| ▲ | TedDoesntTalk 11 hours ago | parent | prev | next [-] | |||||||||||||||||||||||||||||||
Cardinality is the easy way to resolve this. If the data has a cardinality of 1, it should be an attribute. If cardinality > 1, it should be a child element/node. | ||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||
| ▲ | dofm 11 hours ago | parent | prev | next [-] | |||||||||||||||||||||||||||||||
> Why is the country name an attribute but not the rank? Perhaps because it's an example of what is possible in XML and how to parse it, and not, in fact, a particularly good or canonical example of XML? | ||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||
| ▲ | cryptos 11 hours ago | parent | prev | next [-] | |||||||||||||||||||||||||||||||
Interesting point of view. JSON is also not the right thing to use in many scenarios, but it is the de-facto standard now. Maybe something like protobuf is the way to go. | ||||||||||||||||||||||||||||||||
| ▲ | mickeyp 11 hours ago | parent | prev | next [-] | |||||||||||||||||||||||||||||||
Because SAX parsing is a thing, and the visitor pattern makes it easy to elide searches in sub-trees if an attribute does not match. So if name == "foobar" then read; else ignore. For a 500 GiB XML file that makes a difference. As for your other point about an "AST" (it's actually just a DOM.) That's the the benefit? And you're in for a surprise when you learn that reaching into a deeply-nested JSON structure deserialised into whatever memory format most appropriate for your pet language is also an abstract data type that you act on with getters/accessors/what-have-yous that is in all but name a DOM. And we do have tools to deal with it: XSLT for transformation. For querying? XPath. | ||||||||||||||||||||||||||||||||
| ▲ | frollogaston 10 hours ago | parent | prev | next [-] | |||||||||||||||||||||||||||||||
I hate the X and L parts too. Just because you put a URL inside doesn't make the other side understand your structure. The features that try to make it extensible actually make it less so. I can't think of any cases XML has helped, and plenty where it's massively gotten in the way. XMPP should've been json for instance. React used something like XML in structure for JSX but didn't actually use XML, so thank goodness we didn't have to put xmlns= all over it. | ||||||||||||||||||||||||||||||||
| ▲ | actionfromafar 11 hours ago | parent | prev | next [-] | |||||||||||||||||||||||||||||||
YAML made me not hate XML. | ||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||
| ▲ | sfn42 11 hours ago | parent | prev [-] | |||||||||||||||||||||||||||||||
Not really. In C# I use a parsing library for which I just write a class and then the library automatically serializes the JSON into an instance of that class. I can do the same thing with XML. Of course it doesn't necessarily go that smoothly with all xml, but as long as the xml is fairly simple like a JSON document would be it's totally fine. It's only when you start to use all the features of xml that don't fit neatly into a class model that it starts to get annoying. But if JSON serves your needs then simple xml does as well. I wouldn't use it because JSON works just fine but it's not as bad as people make it seem, unless people make it really bad. | ||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||