开发者

Proper use of XML

Over the past couple years, I have seen a lot of ar开发者_JAVA百科ticles, opinions on how XML is an overkill or incorrectly used for .

When exactly should I use XML? What specific examples are there that your average programmer may run in to that, without a doubt XML is one of the best solutions? Are there any good articles/books that addresses this issue (not articles on what XML is NOT suited for)?


XML was designed primarily as a document markup language, and its widespread adoption for exchanging structured data was something of a surprise (to me, at least). It's worth recalling why that adoption happened (compared with pre-existing options such as CSV, ASN.1, or custom formats such as EDI and Swift). It was a format that supported Unicode, and was rich enough to represent fairly complex data; there were free off-the-shelf parsers available, it was human-readable which makes debugging easy, and it could be validated using a schema language (indeed, a choice of schema languages). It comes with its own high-level declarative processing languages in the form of XSLT, XPath, and more recently XQuery. But it did carry baggage from its origins in the world of documents - baggage that is important for some data-oriented applications (because in truth, most data has narrative text in it somewhere), but not for all. It was never designed with the constraints of existing programming languages in mind, and that is the biggest reason people seek alternatives. JSON and YAML provide many of the original benefits without the baggage, but not all - for many applications, the lack of a powerful schema language is a serious drawback. However, if you want a language primarily for exchanging data between procedural programs, with no need for a schema, and with no need for rich text, then JSON might well suit you better,


I use xml IFF i need a human readable (for certain definitions of readable), hierarchical, verifiably (schema/relax), interoperable( from app to app or even serialized objects) storage format.

I also use it when the goal is to allow 3rd party tools to transform the data into other presentations (xsl->docbook,xhtml, etc).

If you need a private/internal data format, xml is about as overkill as you can get.


The beauty of XML is that it allows you to transfer structured information between systems that could not otherwise communicate efficiently. Say between two databases for instance. Thus a valid use for XML can be made in this case and XML parsers and output functions are typically implemented within DBMS solutions. Perhaps my answer is too contrived to answer a question as broad as yours but I believe the value that XML brings to the table is it's ability to share structured information between incompatible technologies.


For the purposes of cross platform interoperability I like the fact that XML, XPath, XML Schema, XSLT and XHTML etc. are standards defined and maintained by W3. Added to that are a whole raft of XML standards (UBL, GML etc). That gives a high degree of confidence that when I use XML for data exchanges between systems I'll get the results I expect and that the many of standards exist to begin with.

While there are some things that JSON is undoubtedly a more efficient solution to XML has a wider range of possibilities. If I was building a single application then I'd consider JSON. If I wanted to build a service oriented architecture spanning many systems, based on a standardised exchange format, then I'd go with XML - as I'd know it would cover pretty much every requirement. In many situations it might be overkill, but in the few where it was able to do things JSON couldn't I'd be glad a I chose the heavyweight approach. If I were building a single part of that architecture I might curse that the architect had chosen XML when my needs were covered by JSON. But there'll be a guy across town who needs all his namespaces and schemas to build his part.


A good use seems to be dependency injection (a la the Spring framework). It allows a lot of flexibility in configuring and re-configuring applications, without ever re-building.

This is the only context in which I've used it, and I find it very beneficial.


I'd have to say that I honestly never use XML anymore. If I require storing properties outside the codebase, I use a property file (key=value). When writing web services, I use JSON as the glue that binds my two applications together.

The one exception was for a project where the vendor preferred XML. They are a .NET shop, and they prefer SOAP and XML. The data I was receiving contained N records of 10 child elements each, and the information is parsed and stored in a database.

For me, the only time I would use XML is to speak the language of other systems that are also speaking XML.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜