我什么时候会使用 XML 而不是 SQL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/201568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When would I use XML instead of SQL?
提问by Patrick Lewis
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.
我从事数据库驱动的 Web 应用程序已经有几年了,最近参与了一个涉及支持 XML 的 CMS 的项目。这让我开始思考 XML/XSLT 的一般用法,以及在什么情况下它比我一直使用的方法更有用,后者将我的所有数据存储在 (My)SQL 数据库中,然后使用PHP/Python/等。根据需要在网络上使用它。
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?
显然有些东西我没有在这里“得到”..谁能给我一些应用程序的例子,在这些应用程序中,将数据存储在 XML 文件而不是数据库中会更可取?
采纳答案by ConcernedOfTunbridgeWells
To quote This Book(Effective XML: 50 Specific Ways to Improve Your XML):
引用这本书(有效的 XML:改进 XML 的 50 种特定方法):
“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.“
“XML 不是数据库。它从来就不是一个数据库。它永远不会成为一个数据库。关系数据库是经过验证的技术,拥有 20 多年的实施经验。它们是坚固、稳定、有用的产品。他们不会离开。XML 是一种非常有用的技术,可用于在不同数据库之间或数据库与其他程序之间移动数据。但是,它本身并不是一个数据库。不要像使用它一样使用它。“
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.
我认为这总结了,如果有点直率的话。XML 是一种数据交换格式。可以拥有 XML 解析库,可以使用 XPath 表达式查询 DOM,但这与 DBMS 不同。您可以使用 DOM/XPath 接口构建 DBMS,但要获得 ACID 属性或扩展到大型数据集,您需要实现 DBMS 引擎和具有索引、日志记录和 DBMS 其他工件的数据格式 - 这(根据定义)使得它不是 XML 的东西。
回答by Veynom
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.
使用 XML 创建需要发送到其他应用程序的文件。XML 更适合作为数据交换格式而不是数据存储格式。
The following link is not bad to describe when using XML: Why should I use XML ?
使用 XML 时,以下链接描述还不错:Why should I use XML ?
回答by James Curran
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 是很好的表格数据——可以轻松放入行和列的数据。XML 适用于分层数据——具有多个不同大小级别的数据。
SQL is good for storage & searching. XML is good for transmitting & formatting.
SQL 非常适合存储和搜索。XML 非常适合传输和格式化。
回答by Dave Markle
1) When you have to interchange your data with others. XML is the "lingua franca" of the Web -- just about everyone can read and interpret it, unlike a database file.
1) 当您必须与他人交换您的数据时。XML 是Web的“通用语言”—— 几乎每个人都可以阅读和解释它,这与数据库文件不同。
2) When your data volume is small and you don't have to do complex queries against it. XML files are good for things like storing configuration or document templates.
2)当你的数据量很小,你不必对它做复杂的查询时。XML 文件适用于存储配置或文档模板等内容。
3) When you don't have many writers trying to access the same data. SQL databases have involved concurrency mechanisms that are working behind the scenes for you. SQL databases can support indexes for the retrieval of information on large data sets quickly...
3)当你没有很多作者试图访问相同的数据时。SQL 数据库涉及在幕后为您工作的并发机制。SQL 数据库可以支持索引,用于快速检索大数据集上的信息...
回答by Robert Rossney
Things I use XML for:
我使用 XML 做的事情:
- 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!)
- 持久化对象层次结构。
- 将数据从一个过程或机器转移到另一个过程或机器。
- 很少变化的数据,如果有的话;配置设置等。
- 作为 XSLT 的输入转换:一般来说,如果我的一个程序发出 HTML,它将使用 XSLT 来完成,因此源数据将在某些时候表示为 XML
- 文本标记。(我们不要忘记这一点!)
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.
具有讽刺意味的是,目前我最频繁地使用 XML 的地方是在桌面应用程序中构建内存中的 ADO DataSet 并使用 DataSet 的 WriteXml 和 ReadXml 方法来持久化和检索它。我使用 ADO 是因为使用 ADO 动态构建由元信息定义的数据模型比为任务实现我自己的对象模型要容易得多。
So here's a case that lookslike 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.
所以这里有一个案例,看起来我正在使用 XML 作为数据库。但我真的不是。我使用的对象模型恰好实现了许多类似数据库的功能,并且我使用 XML 作为其持久性格式。
回答by baash05
I believe there are quite a few commercial applications that make heavy use of XML as a storage medium. I've done it for a project planning application, where the user stores each project in it's own file. The app lives on a USB stick, and requires zero install. All the data is pulled from the XML and worked on in memory, so getRecord(id) is nuts fast.
我相信有相当多的商业应用程序大量使用 XML 作为存储介质。我已经为一个项目规划应用程序完成了它,用户将每个项目存储在它自己的文件中。该应用程序存在于 USB 记忆棒上,并且需要零安装。所有数据都从 XML 中提取并在内存中处理,因此 getRecord(id) 非常快。
So my answer would be.. when the data is small enough to be held in memory, a database is over kill.
所以我的答案是......当数据小到可以保存在内存中时,数据库就结束了。
回答by TRiG
回答by DOK
Here is an example of using XML with SQL: Authenticated users read and write data to various databases, not all of which are the same DBMS. Users for Company A use data from a local SQL Server database. Users for Company B use data from a remote Oracle database. And so on. A dozen different databases, each with slightly different schemas for the same basic data.
以下是将 XML 与 SQL 结合使用的示例:经过身份验证的用户向各种数据库读取和写入数据,并非所有数据库都是相同的 DBMS。公司 A 的用户使用本地 SQL Server 数据库中的数据。公司 B 的用户使用来自远程 Oracle 数据库的数据。等等。十几个不同的数据库,对于相同的基本数据,每个数据库的模式略有不同。
The website developer does not have the ability to create stored procedures on the remote databases. SQL must be sent directly from the web app to the database. Since each database has a slightly different SQL syntax and schema, it is necessary to use different SQL for each of the 12 databases for the same operation (SELECT, INSERT, etc).
网站开发人员无法在远程数据库上创建存储过程。SQL 必须直接从 Web 应用程序发送到数据库。由于每个数据库的 SQL 语法和模式略有不同,因此需要对 12 个数据库中的每一个使用不同的 SQL 进行相同的操作(SELECT、INSERT 等)。
One of the choices for embedding the SQL statements in the web app is to place them in XML files. Each XML file contains the set of SQL statements for one of the dozen databases. The code determines which database is accessed for the logged-in user, and retrieves the appropriate SQL from the specified XML file.
在 Web 应用程序中嵌入 SQL 语句的选择之一是将它们放在 XML 文件中。每个 XML 文件都包含十几个数据库之一的 SQL 语句集。该代码确定登录用户访问哪个数据库,并从指定的 XML 文件中检索适当的 SQL。
Just as with stored procedures, the SQL in the XML file can be updated without stopping or recompiling the application.
与存储过程一样,无需停止或重新编译应用程序即可更新 XML 文件中的 SQL。
回答by IAmCodeMonkey
Both XML and RDMSs can be used as datastores, but each implementation has its own advantages and drawbacks.
XML 和 RDMS 都可以用作数据存储,但每种实现都有自己的优点和缺点。
Using XML to store data for a web application is usually not a big problem until you start dealing with large amounts of data or you decide that you want to discover other information from your data (example: data mining). In other words, storing large amounts of XML files for a datasource is not very scalable but it makes it easier to move the data around. XML can also be used to serialize complex objects in a non-relational format, which could eliminate the need for an ORM if you can serialize/deserialize your objects directly from the XML
在您开始处理大量数据或决定要从数据中发现其他信息(例如:数据挖掘)之前,使用 XML 为 Web 应用程序存储数据通常不是什么大问题。换句话说,为数据源存储大量 XML 文件的可扩展性不是很强,但它可以更轻松地移动数据。XML 还可用于以非关系格式序列化复杂对象,如果您可以直接从 XML 序列化/反序列化对象,则可以消除对 ORM 的需要
RDMSs (databases) are usually more scalable, offer greater concurrency support and are much faster when working with large amounts of data. The relational model makes it easier to data mine later on. Databases do suffer from the object-relational impedance mismatch (http://en.wikipedia.org/wiki/Object-Relational_impedance_mismatch) which might require you to write ugly code or use complex ORMs.
RDMS(数据库)通常具有更高的可扩展性,提供更大的并发支持,并且在处理大量数据时速度更快。关系模型使以后的数据挖掘更容易。数据库确实存在对象关系阻抗不匹配 ( http://en.wikipedia.org/wiki/Object-Relational_impedance_mismatch),这可能需要您编写丑陋的代码或使用复杂的 ORM。
回答by billb
Whenever you don't have the luxury of having a database (think single user applications) or need a very lightweight storage format.
每当您没有数据库(想想单用户应用程序)或需要非常轻量级的存储格式时。
Also as the previous poster mentioned, an interchange format.
同样如上一个海报所述,一种交换格式。