What is XML good for and when should i be using it

2019-03-17 18:42发布

问题:

I'm curious, I've been developing pretty powerful websites/web apps, and I've never learned XML, even odder I've never really felt the need to. It's not like curl or Prepared Statements where, before knowing what they did and how they worked, I had a feeling 'there's got to be an easier way to do this!' or 'there's got to be something designed for this!'.

Currently I work with MySQL and JSON and I don't have this feeling of 'I need to learn that' (XML), this must be wrong!

I'm really interested to hear some compelling arguments for XML, and learn about things which it can do beter than JSON or MySQL (or some other aspect of web dev) and when i should be using it!

回答1:

JSON is very lightweight which makes it better suited for passing data around to the front end.

XML has descriptive tags that (I personally find) make it easier to read in a raw format. If I wanted to have any sort of settings file that is loaded in from my program, i would have it in an XML file format.

That's my idea of it anyway, but i'm sure there are much more in-depth reasons for choosing one over the other. Of which i am not experienced enough to list :)

However i did find a couple of sites that make some good points.

http://ajaxian.com/archives/json-vs-xml-the-debate (Some good points in the comments)

http://webignition.net/articles/xml-vs-yaml-vs-json-a-study-to-find-answers/



回答2:

XML is useful for storing heterogeneous tree structures, in situations where general purpose tools can be applied to them and some redundancy is desirable. If you are doing modern web development, there is a good chance you are producing XHTML rather than HTML, and are producing RSS or Atom, so you should already using be it. The most common RDF formats use it.

JSON is a bit easier to work with for data on the web, but hasn't got the same feature set - you can't have attributes in JSON so there is no implicit difference between data and meta-data, and you don't have processing instructions or the ability to create entities for repeated chunks of text. On the other hand, many uses of XMLl don't use those features either. SQL databases have a fixed schema, and do not represent trees well.

Mostly XML is used for interoperability.



回答3:

One of the advantage of XML over other serialization formats is the number of tools available. The other is the ability to formalize the description of you data (XML Schema).

The availability of tools lets you use XML editors, transformers, visualizers, ... For example, where I work, we have the communication team using an XML editor to edit content and metadata. They are not technical enough to write JSON by hand (or XML), but it is very easy to give them a template with a nice generic frontend to edit the needed documents.

Having a way to describe the format (XSD, DTD, Relax NG, ...) means that you can also automagically validate your documents. It also serves as a pretty good documentation of what is allowed and what is not in your documents.



回答4:

XML is simply for storing messages in a structured way that is (ostensibly) application agnostic. This is all it is. Said another way, XML offers a way to preserve semantics (meaning) of data when communicating between different applications. It's also popular as a configuration format since (1) a config file is just a message between different application sessions* and (2) almost every language has mature, standard XML libraries.

*you can also think of this as just a degenerate case of communicating between applications.



回答5:

I use XML for translations of web site labels, tags etc, or non-repetitive content. For this kind of thing, it's a life saver.



回答6:

I use XML mainly for config files or as transportation format, however if you are familiar with JSON, or YAML, they might be just as fine for you, so there is no real need to learn XML.



回答7:

XML is the only solution for the data interchange and good for nothing else.

So, you have to learn XML only if you gonna parse or supply an RSS feed. No rocket science though, as it is same markup language as HTML, with some strict guidelines.

Ol' good article from ol' good Joel to sort things out



回答8:

"Knowing XML" can mean a couple of different things.

The first is understanding the basic syntax. It is a prerequisite for writing XHTML, SVG, Atom, RSS, and a host of other languages which are XML applications.

The second builds upon the first and is an understand of how to develop your own XML applications, i.e. custom data storage or exchange formats. JSON can fulfill a similar role and has some advantages (such as being able to implicitly represent an array of data: { bar: ['foo'] }. In XML a parser would have to know to convert the contents of <bar> into an array for the programming language if you want to treat it as a simple data structure) <bar><foo/></bar>) and disadvantages (XML lets you have optional things in any order with less effort).



回答9:

XML is useful as a format for:

  • porting data across platforms
  • backup or export text from a database
  • cross-language integration

Content management systems use it for this purpose. It's also useful for caching client-side templating code, as used on skechers.com and explained on in a comment from Mark Beeson on Tim Bray's site:

Luckily, there is a way to cache the entire view layer on the client, while still respecting pages and still offering AJAX in correct places without monstrous URLS. Check out the source of skechers.com -- we're using client-side XSLT, which gets cached in the browser, and the one xsl file describes the layout of the entire site. That way, when navigating from page to page, you're only downloading a small amount of XML. We found that this speeds up our app immensely, and allows us to pull off a massive, massive amount of caching.

References

  • MediaWiki: Export
  • WordPress: Tools Export Screen
  • mysqldump XML format
  • XML Quotes
  • XML: The Angle Bracket Tax
  • Is XML too Hard?
  • XML is not S-Expressions
  • Is XML Human Readable?


回答10:

With PHP, as with most dynamic languages, it's best relegated to interoperability purposes. XML is faster to rewrite than Java, but PHP is faster to rewrite than XML.