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".
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.
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.
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?
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.
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, an sql database is used, when something simpler would suffice. An Sql database is not corruption or hack-proof either, and it ALWAYS comes down to the implementation.