Page 1 of 1
Generating XML file
Posted: Fri Apr 03, 2009 8:26 am
by robbb99
Hello,
Let me start from the top: I have a MYSQL database and have managed to generate an XML file using PHP (
http://www.robmaps.co.uk/Floor_plans/Ch ... links2.php). I have also got the necessary code which enables me to perfrom a 'live search' feature on my site, however, this code involves reading an XML
file, which I dont have. A snippet of PHP that reads the file:
Code: Select all
$xmlDoc = new DOMDocument();
$xmlDoc->load("links2.xml");
$x=$xmlDoc->getElementsByTagName('link')
So I can generate XML string but I need to be able to have an dynamic XML file which will update when any changes are made to the MYSQL database. How can i save the XML data to file? or What code can I use instead of the above to import the XML string?
Hope I've made it clear.
Thanks for your help,
Rob
Re: Generating XML file
Posted: Fri Apr 03, 2009 10:53 am
by Reviresco
Re: Generating XML file
Posted: Fri Apr 03, 2009 2:20 pm
by robbb99
Thanks for the reply. I have heeded your advice, except I used fopen() instead of simplexml.
OK, please bear with me, i'm new to PHP. So this is what I have now got:
Code: Select all
<?php
require('databaseconnection.php');
// Select all the rows in the markers table
$query = "SELECT * FROM chadwickinfo WHERE 1";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
header("Content-type: text/xml");
// Start XML file, echo parent node
echo '<pages>';
// Iterate through the rows, printing XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
echo "<link>";
echo "<title>" . $row["name"] . "</title>";
echo "<url>" . $row["photo"] . "</url>";
echo "</link>";
}
// End XML file
echo '</pages>';
$xmlfile = "links2.xml";
$file = fopen($xmlfile,"w");
fwrite($file, "<xml>");
fwrite($file, "<link><title>" . $row["name"] . "</title><url>" . $row["photo"] . "</url></link>");
fwrite($file, "</xml>");
fclose($file);
?>
It still generates the XML string when previewed in the browser, however, I cant see any data added to the "links2.xml" file. Is that last bit of code correct?
Thanks,
Rob
ps I'm using Dreamweaver 8.0, is this sufficient? What is the best PHP program to use?
Re: Generating XML file
Posted: Fri Apr 03, 2009 2:25 pm
by php_east
use browser's "view source" to view xml data.
Re: Generating XML file
Posted: Fri Apr 03, 2009 3:25 pm
by Reviresco
Is the file not being created, or is it being created but there's nothing written to it?
Here's how I would do it:
Code: Select all
$xml = "<?xml version="1.0" encoding="UTF-8"?><link><title>" . $row["name"] . "</title><url>" . $row["photo"] . "</url></link>";
$xmlfile = "links2.xml";
$file = fopen($xmlfile,"w") or die("Can't open file");
fwrite($file, $xml);
fclose($file);