什么时候使用XML而不是SQL?

时间:2020-12-27 20:55:49

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/等等。根据需要在web上使用它。

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文件而不是数据库中存储数据会更好?

13 个解决方案

#1


91  

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以外的东西。

#2


21  

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时,下面的链接描述得还不错:为什么我应该使用XML ?

#3


14  

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有利于传输和格式化。

#4


10  

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数据库可以支持索引,以便快速检索大型数据集上的信息……

#5


5  

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
  • 作为XSLT转换的输入:一般来说,如果我的一个程序发出HTML,它使用XSLT进行转换,因此源数据在某些情况下将表示为XML
  • 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.

具有讽刺意味的是,我目前对XML使用最多的地方是在一个桌面应用程序中,该应用程序构建一个内存中的ADO数据集,并使用数据集的WriteXml和ReadXml方法来保存和检索它。我使用ADO是因为使用ADO动态构建metainformation定义的数据模型要比为任务实现我自己的对象模型容易得多。

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.

这里有一个例子,我用XML作为数据库。但我真的不是。我使用的对象模型碰巧实现了许多类似数据库的功能,我使用XML作为其持久性格式。

#6


2  

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作为存储介质。我已经为一个项目规划应用程序做过,用户将每个项目存储在它自己的文件中。该应用程序使用u盘,并且需要零安装。所有数据都从XML中取出并在内存中工作,因此getRecord(id)非常快速。

So my answer would be.. when the data is small enough to be held in memory, a database is over kill.

所以我的答案是。当数据足够小,可以存储在内存中时,数据库就会超负荷运行。

#7


1  

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.

正如前面提到的,交换格式。

#8


1  

Both XML and RDMSs can be used as datastores, but each implementation has its own advantages and drawbacks.

XML和RDMSs都可以用作数据存储,但是每个实现都有自己的优缺点。

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.

RDMSs(数据库)通常更可伸缩,提供更大的并发支持,并且在处理大量数据时速度更快。关系模型使以后的数据挖掘更加容易。数据库确实受到了对象-关系阻抗不匹配的影响(http://en.wikipedia.org/wiki/object - relational_impedance_),它可能要求您编写丑陋的代码或使用复杂的orm。

#9


1  

I would go if I have limited mysql databases on my host, then I would see the opportunity for using XML as a datastore.

如果我的主机上只有有限的mysql数据库,那么我将看到使用XML作为数据存储的机会。

#10


1  

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服务器数据库的数据。公司B的用户使用来自远程Oracle数据库的数据。等等。12个不同的数据库,每个数据库的基本数据模式略有不同。

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。

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文件都包含12个数据库中的一个的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可以在不停止或重新编译应用程序的情况下进行更新。

#11


1  

Don't.

不喜欢。

Try SELECT author FROM book

尝试从书中选择作者

#12


0  

Some applications use XML files to store configuration, I prefer to use SQLite to do so.

有些应用程序使用XML文件来存储配置,我更喜欢使用SQLite。

#13


0  

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

我永远不会使用任何XML来存储我的数据。

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.

我们在SOA应用程序中使用XSLT转换作为数据抽象层。所有对象都用XSLT相互发送数据,所以它们只需要理解一种语言。除了数据库连接器,它需要能够将数据转换为SQL,以便将数据发送到DB。

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

这样,您的应用程序中就不会有分布在无数对象上的SQL字符串生成。

#1


91  

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以外的东西。

#2


21  

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时,下面的链接描述得还不错:为什么我应该使用XML ?

#3


14  

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有利于传输和格式化。

#4


10  

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数据库可以支持索引,以便快速检索大型数据集上的信息……

#5


5  

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
  • 作为XSLT转换的输入:一般来说,如果我的一个程序发出HTML,它使用XSLT进行转换,因此源数据在某些情况下将表示为XML
  • 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.

具有讽刺意味的是,我目前对XML使用最多的地方是在一个桌面应用程序中,该应用程序构建一个内存中的ADO数据集,并使用数据集的WriteXml和ReadXml方法来保存和检索它。我使用ADO是因为使用ADO动态构建metainformation定义的数据模型要比为任务实现我自己的对象模型容易得多。

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.

这里有一个例子,我用XML作为数据库。但我真的不是。我使用的对象模型碰巧实现了许多类似数据库的功能,我使用XML作为其持久性格式。

#6


2  

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作为存储介质。我已经为一个项目规划应用程序做过,用户将每个项目存储在它自己的文件中。该应用程序使用u盘,并且需要零安装。所有数据都从XML中取出并在内存中工作,因此getRecord(id)非常快速。

So my answer would be.. when the data is small enough to be held in memory, a database is over kill.

所以我的答案是。当数据足够小,可以存储在内存中时,数据库就会超负荷运行。

#7


1  

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.

正如前面提到的,交换格式。

#8


1  

Both XML and RDMSs can be used as datastores, but each implementation has its own advantages and drawbacks.

XML和RDMSs都可以用作数据存储,但是每个实现都有自己的优缺点。

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.

RDMSs(数据库)通常更可伸缩,提供更大的并发支持,并且在处理大量数据时速度更快。关系模型使以后的数据挖掘更加容易。数据库确实受到了对象-关系阻抗不匹配的影响(http://en.wikipedia.org/wiki/object - relational_impedance_),它可能要求您编写丑陋的代码或使用复杂的orm。

#9


1  

I would go if I have limited mysql databases on my host, then I would see the opportunity for using XML as a datastore.

如果我的主机上只有有限的mysql数据库,那么我将看到使用XML作为数据存储的机会。

#10


1  

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服务器数据库的数据。公司B的用户使用来自远程Oracle数据库的数据。等等。12个不同的数据库,每个数据库的基本数据模式略有不同。

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。

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文件都包含12个数据库中的一个的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可以在不停止或重新编译应用程序的情况下进行更新。

#11


1  

Don't.

不喜欢。

Try SELECT author FROM book

尝试从书中选择作者

#12


0  

Some applications use XML files to store configuration, I prefer to use SQLite to do so.

有些应用程序使用XML文件来存储配置,我更喜欢使用SQLite。

#13


0  

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

我永远不会使用任何XML来存储我的数据。

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.

我们在SOA应用程序中使用XSLT转换作为数据抽象层。所有对象都用XSLT相互发送数据,所以它们只需要理解一种语言。除了数据库连接器,它需要能够将数据转换为SQL,以便将数据发送到DB。

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

这样,您的应用程序中就不会有分布在无数对象上的SQL字符串生成。