Remix.run Logo
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:

    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
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.

conartist6 11 hours ago | parent | next [-]

CSTML gets the best of both worlds https://docs.bablr.org/guides/cstml

gf000 10 hours ago | parent | prev [-]

Well, "easier" may well be "one less dimension to encode data" in this case.

Sure, this gives quite a few variations on how to serialize some data. But it's not like json's simpler approach would make data serialization universal, there are many different ways to encode the same thing.

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.

throw310822 10 hours ago | parent [-]

Not sure I understand. How do you represent an object nested in another object?

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?

woodruffw 11 hours ago | parent [-]

I think GP’s question was rhetorical. They know it’s an example.

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.

SoftTalker 11 hours ago | parent | next [-]

Agreed. Among text based formats, nothing I hate more than YAML.

wombatpm 11 hours ago | parent | prev | next [-]

XML is like violence. If it’s not solving your problem, you need to use more.

tonyedgecombe 11 hours ago | parent | prev | next [-]

That and all the proprietary formats we had to deal with before XML came along.

2 hours ago | parent | prev [-]
[deleted]
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.

kccqzy 11 hours ago | parent | next [-]

That is actually a good approach that I have also used a lot: let the parsing library handle everything including the serialization and deserialization. But if you do that, why do you care that behind the scenes it is using JSON or XML or protobuf or something else?

ExoticPearTree 10 hours ago | parent [-]

Because at some point you have to debug stuff. And JSON is easier to read than XML to figure out where a problem might be.

gf000 10 hours ago | parent | prev [-]

I would even go as far to say that XML may very well be better in some cases, - here you have a schema most of the time, so you can often catch e.g. schema evolution failures at compile time.

This is much less common/less standardized with json.

magicalhippo 10 hours ago | parent [-]

JSON schema exists. If you restrict yourself to a sensible subset of XML features in your XML schema, you can have a 1:1 correspondence to JSON and JSON Schema. We do that at work. Due to historical reasons we have a XSD but provide the complimentary JSON Schema to those who wish to send us JSON.

The JSON is converted on the fly to XML based on the XSD so it can be ingested by our existing XML integration. Similar with return answers, response XMLs are converted on the fly based on the XSD to JSON.

JSON endpoints validate against the JSON Schema, also generated from the XSD at runtime, XML against the XSD of course.

We had a diverse set of XSDs but didn't have to tweak them to support JSON. We used restrictions and extensions, both simple and complex, we used min/max, enums, descriptions and examples and more, so not entirely boring XSDs.

We did establish some conventions, attributes turns the child element to an object and the attributes become properies, just simple stuff like that.

This way customers can hand us what they prefer generating and ingesting, and we don't have to worry about keeping two different schemas for the same endpoint in sync.