views:

2767

answers:

13

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 got me to thinking 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?

+12  A: 

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 ?

Veynom
Good fan-out. +1
ConcernedOfTunbridgeWells
A: 

See my answer here

Galwegian
A: 

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
+2  A: 

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
Mod +1 for "lingua franca"
+35  A: 

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
+4  A: 

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
A: 

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.

IAmCodeMonkey
A: 

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

CMS
A: 

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
A: 

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
"All objects send each other data in XSLT,"Seriously?
Goran
All those communicating over the service bus.
Treb
XSLT is the language for transforming the XML not to carry information. I imagine you could use XSLT to send data as it is a subset of XML but why would you want to?
Goran
Yes, you're right - I need to rephrase that.
Treb
A: 

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
A: 

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
+2  A: 

Don't.

Try SELECT author FROM book

TRiG