I was wondering how i should go about writing an XML data layer for a fairly simple php web site. The reasons for this are:
- db server is not available.
- Simple data schema that can be expressed in xml.
- I like the idea of having a self contained app, without server dependencies.
- I would possibly want to abstract it to a small framework for reuse in other projects.
The schema resembles a simple book catalog with a few lookup tables plus i18n. So, it is quite simple to express.
The size of the main xml file is in the range of 100kb to 15mb. But it could grow at some point to ~100mb.
I am actually considering extending my model classes to handle xml data. Currently I fetch data with a combination of XMLReader and SimpleXml, like this:
public function find($xpath){
while($this->xml_reader->read()){
if($this->xml_reader->nodeType===XMLREADER::ELEMENT &&
$this->xml_reader->localName == 'book' ){
$node = $this->xml_reader->expand();
$dom = new DOMDocument();
$n = $dom->importNode($node, true);
$dom->appendChild($n);
$sx = simplexml_import_dom($n);
// xpath returns an array
$res = $sx->xpath($xpath);
if(isset($res[0]) && $res[0]){
$this->results[] = $res;
}
}
return $this->results;
}
So, instead of loading the whole xml file in memory, I create a SimpleXml object for each section and run an xpath query on that object. The function returns an array of SimpleXml objects. For conservative search I would probably break on first found item.
The questions i have to ask are:
- Would you consider this as a viable solution, even for a medium to large data store?
- Are there any considerations/patterns to keep in mind, when handling XML in PHP?
- Does the above code scale for large files (100mb)?
- Can inserts and updates in large xml files be handled in a low overhead manner?
- Would you suggest an alternative data format as a better option?