Why is XML used in web services?

你说的曾经没有我的故事 提交于 2019-12-02 15:39:30

问题


I am, for the first time, writing a program in which needs to communicate with other programs, using web services, specifically the SOAP V1.2 and HTTP protocols. My employer keep asking me three things that I can't really answer:

  • Why is XML used as the convention (W3C) for information exchange?
  • Why can't they use plain text or delimited files, for example?

Why use a repetitive heavy tag-based language to exchange info? What happened to the good ol' plain text?


回答1:


XML has the potential to transfer self-descriptive data, and has a large following.

Plain text, or simple delimited files, tell you nothing about the data contained in the payload.

This makes it less desirable in cases where adequate domain knowledge cannot be assumed on the part of the web service consumer.

JSON is kind of a hybrid approach - more descriptive than plain text, or delimited files (I'm assuming you mean something like a CSV file), but less onerous to deal with, simpler for a human to read, and more elegant (in some opinions) than XML.

If you're looking for the "right" answer for your boss, well, there really isn't one. The standards for 'conversation' between a web service publisher and consumer depends on a whole host of factors - was the API decided before you came along? does the other side deal only with technology X? Any number of factors could have decided the data exchange format.

If you want REAL fun, try working with EDX trading partners - ugh.




回答2:


Before XML there were lots of plain text formats for sending messages. Each was different, most of them were poorly specified, so it was never 100% clear what was allowed and what wasn't. People designing such formats ended up putting lots of effort into designing the syntax, leaving not enough time to go into designing the semantics. Anyone receiving data in such a format had to write their own parser. Most people aren't good at writing parsers (they skipped those lectures in their computer science education, or they never had a computer science education in the first place), so they did it badly and got it wrong.

So XML arrived and did away with all that. People designing messages could now concentrate on the semantics of the data rather than inventing yet another syntax. People writing code to read and write messages could use XML libraries "off the shelf" rather than hand-crafting their own. Schema languages were invented that allowed the validation code to be automated. High-level transformation and query languages were invented so you didn't have to grovel around the data in low-level procedural code.

And God saw that it was good. Well, Microsoft, Sun, Oracle, and IBM did, and that was good enough for most people.



来源:https://stackoverflow.com/questions/23485732/why-is-xml-used-in-web-services

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!