One thing you guys are missing is that he needs the xml document to generate the page with xslt. However, I do agree that the data should be stored in a database for easier manipulation. Also, those xml functions don't appear to be supported by x10. XML parsing and the XMLWriter object are supported, but to do something like this they're a bit unwieldy.
Personally, I would say that given these circumstances you should store all the data in the db and regenerate the xml document whenever it changes. However, this may require changing a lot of things with your current system. So for now I'll just give you a rather simple function to accomplish what you want:
PHP:
function append_xml($file, $content, $sibling, $single = false) {
$doc = file_get_contents($file);
if ($single) {
$pos = strrpos($doc, "<$sibling");
$pos = strpos($doc, ">", $pos) + 1;
}
else {
$pos = strrpos($doc, "</$sibling>") + strlen("</$sibling>");
}
return file_put_contents($file, substr($doc, 0, $pos) . "\n$content" . substr($doc, $pos));
}
This function works by taking the name/path of an xml document, the xml you want written to it, and the element it should be added after. So if you had an xml document named products.xml and it looked like this:
Code:
<?xml version="1.0" encoding="UTF-8" ?>
<products>
<product>
<id>1</id>
<name>Product 1</name>
<price>21.00</price>
</product>
<product>
<id>2</id>
<name>Product 2</name>
<price>42.00</price>
</product>
</product>
You could add another product element with this code (provided that the document is stored in the same directory as the script):
PHP:
$content = '<product><id>3</id><name>Product 3</name><price>63.00</price></product>';
append_xml('products.xml', $content, 'product');
The last parameter in the function, $single, should be set to true if you're appending the content after a single-tag element(e.g., <element />). For example if your xml document looked like this instead:
Code:
<?xml version="1.0" encoding="UTF-8" ?>
<products>
<product id="1" name="Product 1" price="21.00" />
<product id="2" name="Product 2" price="42.00" />
</products>
You would change the function call to this:
PHP:
append_xml('products.xml', $content, 'product', true);