107

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?

Patrick Lewis
  • 2,131
  • 2
  • 17
  • 16
  • 5
    I think the question is more accurately XML vs. a RDBMS, not vs. SQL. – JAL Dec 30 '09 at 11:46
  • http://en.wikipedia.org/wiki/Xml_database – Max Toro Jan 24 '10 at 22:59
  • 5
    I think the question is more accurately FILE vs. a RDBMS, not XML vs SQL. – pfeds Feb 25 '13 at 06:30
  • If you have time, recent Turing Award winner Michael Stonebraker's ["What Goes Around Comes Around"](http://idke.ruc.edu.cn/seminars/phd/2007/11.07/What%20Goes%20Around%20Comes%20Around.pdf) is an excellent paper on this history of databases, including how XML databases repeated past mistakes. – Jeffrey Bosboom Jun 27 '15 at 20:26

13 Answers13

107

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.

ConcernedOfTunbridgeWells
  • 64,444
  • 15
  • 143
  • 197
  • 1
    I realise this answer was written in 2008, but with the advent of a number of file-based database systems I find myself wondering if they make more sense than conventional RDBMS in a number of situations. SQL was designed in the dark ages when disk space was costly, and it's become a way of thought within the software industry. That doesn't mean it's right in terms of modern methods. – pfeds Feb 25 '13 at 06:32
  • That still doesn't make XML a database, just a serialisation format for something you're storing in a BLOB. This may be useful to you but it doesn't make XML a substitute for a full featured query facility. – ConcernedOfTunbridgeWells Feb 25 '13 at 10:22
  • 2
    The more efficient disk-space management is not the only advantage of relational databases. Most relational databases are working with fixed field- and record-lengths which makes them able to respond in a constant time to any request while XML-, Yaml-, JSon- and other text-based data storages are basically text-based, so all queries are performed trough text parsing which has a huge overhead in processor time, and can be performed in unpredicatable amount of time. This only could be worked around with a costy (in the terms of CPU time) prebuffering with huge memory requirements. – mg30rg Nov 28 '13 at 08:43
  • If I write a SQL SP to retrieve the data and use HTML to display instead of XSLT file, is it faster or just about the same? – SearchForKnowledge Oct 21 '14 at 14:09
  • 1
    nowadays you can use XQuery for XML databases. – Don Dilanga Jul 08 '18 at 14:37
24

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 ?

Thronk
  • 624
  • 14
  • 37
Veynom
  • 4,079
  • 2
  • 19
  • 24
17

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.

James Curran
  • 101,701
  • 37
  • 181
  • 258
13

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.

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.

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...

Dave Markle
  • 95,573
  • 20
  • 147
  • 170
6

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.

Robert Rossney
  • 94,622
  • 24
  • 146
  • 218
4

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.

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

baash05
  • 4,394
  • 11
  • 59
  • 97
4

Don't.

Try SELECT author FROM book

TRiG
  • 10,148
  • 7
  • 57
  • 107
2

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.

billb
  • 3,608
  • 1
  • 32
  • 36
2

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

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

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.

John Topley
  • 113,588
  • 46
  • 195
  • 237
IAmCodeMonkey
  • 1,576
  • 1
  • 11
  • 11
2

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

milot
  • 1,060
  • 2
  • 8
  • 17
2

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.

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).

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.

Just as with stored procedures, the SQL in the XML file can be updated without stopping or recompiling the application.

DOK
  • 32,337
  • 7
  • 60
  • 92
1

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

Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
1

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.

Treb
  • 19,903
  • 7
  • 54
  • 87