tags:

views:

134

answers:

6
+1  Q: 

XML as a database

I've been reading about XML, XSL, XPath, etc. I want to start this small script in PHP using XML as the database to create a simple CRUD app. I will be using SimpleXML but after reading some answers over stackoverflow it seems many people suggest DOMDocument (is there anything that benefits using DOMDocument more than SimpleXML?)

Anyway, what bothers me is how to put an ID on them? Should I just start with "1" and then when someone adds a node, increment it and so on?

There is also category, I want to put a have this kind of structure

Parent  
 - Children          
 - Children  
   - Subchildren  

I have an idea on how to put it in XML but the problem for me is parsing them.

Anyway, categories doesn't really bother me that much. I'm more interested to know if I should just keep an IDs counts saved in a XML and use that as "reference".

A: 

IMO i would keep away from from keeping an id count in the xml. If you want to keep it cached as a var in the script i think that would be ok - then just do a count on that particular element as needed the first time you add a new element of that type. Otherwise you could potentially have issue if the id count in the file doesnt actually math the id count of the elements. However if this is an app with multiple users you may be intering race condition territory because you may not always have an accurate count if any thing has been written to disk while someone else is still working. You may want to cache the counts in a simple text file or something.

prodigitalson
A: 

Well, I won't help you, but I'll give you advice: just don't. XML is very slow comparing to, for example SQLLite. Why do you want XML?

radex
so helpful :), because I'm not interested in using MySQL, SQLite or any type of DBMS wanting to get my hands dirty with XML and it uses
allenskd
It entirely depends on what you need/what you want to achieve. If you need a database, use a database. If you want to experiment with XML, then find an area where XML is useful and widely used. Web services, for example.
Josh Davis
XML is very helpful for config, etc, but IMHO not so good choice for "real" database...
radex
+4  A: 

You said "XML as the database." XML is just a markup language, so what you really meant was "a file as the database" and that would be a mistake. You'll run into concurrency problem, possible corruption and a ton of other problems, let alone the abysmal performance of a big XML document.

If what you need is a database, then just use that: a database. Not a file.

SQLite is simple to use and fast enough for light loads, although it kind of lacks a good counterpart to phpMyAdmin. Otherwise, your webhost certainly offers a MySQL database.

Josh Davis
Yes, it was a silly mistake in the title :) Nevertheless whilst the recommendation of using certain RDBMS I still will go ahead. Not because I'm a stubborn person ignoring all the obvious answers but because I just need to try it, it's not going to be for either a commercial product, etc etc. Just testing
allenskd
Well, then the answer is: SimpleXML, not DOM. If you need DOM methods, for example if you need to remove nodes by ID, try SimpleDOM - http://code.google.com/p/simpledom/
Josh Davis
Also, since you have no concurrency control, don't try to create consecutive IDs and use PHP's uniqid() instead.
Josh Davis
+1  A: 

As an exercise this is sort of interesting, but everyone telling you not to use XML for this is right. An RDBMS is the typical best solution for storage you're reading from and writing to.

As for your problem, I would be likely to add an id attribute to any node I cared about having an id for. Then, if I used the DOM extension, I'd do setAttribute, or if I were using SimpleXML, I'd use addAttribute.

But there are even more solutions to dealing with XML in PHP. I'd have a look at many and see which meet your needs best.

Best of luck.

artlung
I do know using XML for storage is bad, yet it intrigues me which is why I wanted to attempt doing it :)
allenskd
A: 

I'm going to go against the grain here and say that there is NOTHING WRONG with using a flat file database, in this case XML

The are pros and cons, and for many the cons seem overwhelming to deal with and give advice that flat file is a bad way to go. It something is wrong, than it is this generalization. A More constructive way would be to point out what the disadvantages rather than denouncing flat file as an option.

There are some successful implementations of a flat file database, for example the getsimple XML based CMS. I think you might be able to poke around that code and se what they are doing with the XML.

I believe that more often than necessary, a mysql database is used, when something simpler would suffice. AmySql database is not corruption or hack-proof either, and it ALWAYS comes doen to the implementation.

Daniel
a developer at my job created a project using a hand-rolled directory tree, flat file xml database. i has about 15,000 records now but it sometimes has some strange behavior and there are like 100,000+ files being stored to maintain the data. its ridiculous. i would recommend using a formal XML database instead of doing your own xml file db.
djangofan
A: 

See This stackoverflow posting for some discussion on a similar topic.

ConcernedOfTunbridgeWells