When would I use XML instead of SQL? [closed]

2020-01-27 09:42发布

I've been working on database-driven web applications for a few years now and recently took on a project involving a CMS that is XML-capable. This has led me to think about the usage of XML/XSLT in general and in what situations it would be more useful than the approach I've always used, which is storing all of my data in a (My)SQL database and then using PHP/Python/etc. to work with it on the web as needed.

There's obviously something I'm not "getting" here.. could anyone give me examples of applications where storing the data in XML files instead of in a database would be preferable?

标签: sql xml
13条回答
Explosion°爆炸
2楼-- · 2020-01-27 10:01

SQL is good tabular data -- data that easily fits into rows & columns. XML is good for hierarchical data -- data which has several levels of different sizes.

SQL is good for storage & searching. XML is good for transmitting & formatting.

查看更多
家丑人穷心不美
3楼-- · 2020-01-27 10:02

Use XML to create files that need to be sent to other applications. XML is more suited as data interchange format than as data storage format.

The following link is not bad to describe when using XML: Why should I use XML ?

查看更多
乱世女痞
4楼-- · 2020-01-27 10:09

To quote This Book (Effective XML: 50 Specific Ways to Improve Your XML):

“XML is not a database. It was never meant to be a database. It is never going to be a database. Relational databases are proven technology with more than 20 years of implementation experience. They are solid, stable, useful products. They are not going away. XML is a very useful technology for moving data between different databases or between databases and other programs. However, it is not itself a database. Don't use it like one.“

I think this sums it up, if a little bluntly. XML is a data interchange format. One can have XML parsing libraries that can query a DOM with XPath expressions but that is not the same thing as a DBMS. You can build a DBMS with a DOM/XPath interface but to get ACID properties or scale to large data sets you need to implement a DBMS engine and a data format with indexes, logging and other artifacts of a DBMS - which (by definition) makes it something other than XML.

查看更多
爷、活的狠高调
5楼-- · 2020-01-27 10:13

Things I use XML for:

  • Persisting an object hierarchy.
  • Moving data from one process or machine to another.
  • Data that changes rarely, if at all; configuration settings and the like.
  • As the input to XSLT transforms: generally speaking, if one of my programs emits HTML, it's using XSLT to do it, and so the source data is going to represented as XML at some
  • Text markup. (Let's not forget that!)

There's not a whole lot of overlap between those use cases and the use cases for a database. Some, but not much.

Ironically, where I'm making my heaviest use of XML at the moment is in a desktop app that builds an in-memory ADO DataSet and uses the DataSet's WriteXml and ReadXml methods to persist and retrieve it. I'm using ADO because it's whole lot easier to dynamically build a data model defined by metainformation using ADO than it would be to implement my own object model for the task.

So here's a case that looks like I'm using XML as a database. But I'm really not. I'm using an object model that happens to implement a lot of database-like functionality, and I'm using XML as its persistence format.

查看更多
ら.Afraid
6楼-- · 2020-01-27 10:14
三岁会撩人
7楼-- · 2020-01-27 10:16

I would never use any kind of XML to store my data.

We use XSLT transformations as a data abstraction layer in our SOA app. All objects send each other data in XSLT, so there is only one language they need to understand. Except for the database connector, which needs to be able to transform the data into SQL, for sending it to the DB.

This way you don't have SQL string generation distributed over a zillion objects in your app. Makes maintenance much easier.

查看更多
登录 后发表回答