views:

100

answers:

6

what is XML and how is it used in databases? if this looks like a homework assignment,it is.

+1  A: 

Databases are often used to store blocks of XML for applications which do not want to serialise it out to an actual database schema. But generally speaking, XML is not used in databases.

This is imply because databases are about storing data where as XML is really about transmitting it in an agnostic manner.

(Simplified) Usually what happens is application A converts data from database A into an XML stream which is transmitted to application B. Application B then converts the XML into a series of SQL statements which create records in database B.

There are lots of other scenarios, but this gives you a general idea.

Derek Clarkson
thanks this helped.
A: 

In general, most of the points of Derek's answer apply.

I would be definitely think over twice before using XML in the database.

Most applications commonly use Relational Databases. However, sometimes, the information in the system is more hierarchical in nature. Storing hierarchy in an RDBMS structure is definitely possible but it takes a lot of manipulation when retrieving it to get it back into its desired structure (things like recursive CTE's in SQL server make this task a bit easier but not necessarily faster)

So, if you come across something like having to store a n level folder hierarchy or maybe a organizational hierarchy, both of which are hierarchical in nature, it is sometimes better to store it as XML. Database usually being the common storage mechanism and since it makes it easier to synchronize changes, hence this data is stored as such in DB though it could potentially be stored just as a flat file too.

Most Relational databases nowdays hence offer a lot of XML support where you can use T-SQL to query the data / update selected nodes etc.

Re: What is XML - for starters look here

InSane
thanks a lot In Sane.
+1  A: 

Xml (Extensible Markup Language) is a mark-up language for encoding documents in a machine-readable format, with emphasis on flexibility, simplicity and usability over the internet.

Its main advantages over other encodings are interoperability - many programming environments have the ability to read and parse xml, and it has the advantage of being human readable. For this reason xml is commonly used when transporting or communicating data (such as on the internet).

The Wikipedia page on xml has a lot more specific information, however some examples of various uses of xml are:

  • RSS, a protocol used to publish feeds of frequently updated content.
  • XHTML, an extension of HTML
  • SOAP (Simple Object Access Protocol), a protocol commonly used for structuring information in web services

Conversely, xml's verbosity and flexible structure comes at the cost of being relatively expensive to read (in terms of computational power) and so xml is not generally used as a storage encoding in performance oriented applications (such as databases).

In terms of how xml would be used in databases - a database might choose to expose data to its client in an xml format or accept data from the client in an xml format. Databases might also be used to store "Blobs" of xml data as records in a table, however this comes at the cost of it being difficult to query the database based on the content of that xml.

Kragen
Thanks Kragen,this hepls a lot
+1  A: 

Derek, In Sane and Kragen's posts are all correct, but I'd like to take it one step further.

Since XML inherently uses a schema (a schema to define what the markup represents), XML all by itself can be considered a database similar to the concept of any other database.

You can define your own schema for XML (an XSD) and then then persist data in your chosen XML format.

True, you can choose to stuff a bunch of XML into a text-field in an SQL database, but you could also use XML all by itself as a database since a database could be nothing more than persisting data in a schema designed to hold that data. There's a gray area here considering even a simple comma-delimited text file could be considered a database if you wrote a program to store and access data in CSV format too.

Anyway, with XML, there are already defined standards for how the schema can be defined (the XSD standards) and parsers are built to conform to those standards so you can put data into and read data out of documents excoded in XML fitting to a named schema (XSD).

So I suppose when you ask about how XML is used in a database, I guess my first question is what exactly do you mean by "database"?

Are you meaning a specific vendor implementation of an SQL database? Or maybe "database" as a logical concept? Or perhaps how XML can be used to enhance interaction with a specific type of database?

Of the answers thus far, I think we're all answering with a different idea of what you mean by "database".

XQUERY, for instance can be used in a standardized way to "query" data in a set of XML files, or in a completely different context XML can be used to easily pass data in and out of Oracle or SQL Server because Microsoft and Oracle provide interfaces which conform to the XML standards.

Allbite
thank you for your reply.
A: 

Ok, when I said "database" I'm generally referring to the databases created by, and accessed through, a relational engine such as Oracle, SQL Server etc.

Yes you certainly can use XML files as means of storing data. It's often done for very small and simple amounts of data. but this is an architectural decision often driven by the amount of effort the developer would have to go to to implement a relational database vs reading and writing XML files, and/or the future usage of that data. Sometimes it's easier to use a XML file, sometimes it's not.

Derek Clarkson
A: 

It's relatively easy to generate XML from relational data. Example: generating an Atom feed from relational data. Example: http://h2database.googlecode.com/svn/trunk/h2/src/test/org/h2/samples/newsfeed.sql

XML or HTML documents (or snippets) are often stored as is (without processing) in databases.

Thomas Mueller