robbb99 Posted April 3, 2009 Share Posted April 3, 2009 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/Chadwick/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: $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 Quote Link to comment https://forums.phpfreaks.com/topic/152385-solved-save-as-xml-file/ Share on other sites More sharing options...
robbb99 Posted April 3, 2009 Author Share Posted April 3, 2009 To put it more clearly: Instead of generating the XML string, how can I save the output as an XML file instead? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/152385-solved-save-as-xml-file/#findComment-800392 Share on other sites More sharing options...
premiso Posted April 3, 2009 Share Posted April 3, 2009 fopen fwrite fclose Simple as A B C. Quote Link to comment https://forums.phpfreaks.com/topic/152385-solved-save-as-xml-file/#findComment-800394 Share on other sites More sharing options...
robbb99 Posted April 3, 2009 Author Share Posted April 3, 2009 Thanks for the reply. I have heeded your advice. OK, please bear with me, i'm new to PHP. So this is what I have now got: <?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? Quote Link to comment https://forums.phpfreaks.com/topic/152385-solved-save-as-xml-file/#findComment-800614 Share on other sites More sharing options...
premiso Posted April 3, 2009 Share Posted April 3, 2009 <?php require('databaseconnection.php'); // Select all the rows in the markers table $query = "SELECT * FROM chadwickinfo WHERE 1"; $result = mysql_query($query); if (!is_resource($result)) { die('Invalid query: ' . mysql_error()); } // header("Content-type: text/xml"); // not needed as we are just writing it to a file. // Start XML file, echo parent node $writeTo = '<pages>'; // Iterate through the rows, printing XML nodes for each while ($row = mysql_fetch_assoc($result)){ // the @ is an error suppressor, I suggest not using it. Instead turn display_errors off in php.ini on your production server. // ADD TO XML DOCUMENT NODE $writeTo .= "<link>"; $writeTo .= "<title>" . $row["name"] . "</title>"; $writeTo .= "<url>" . $row["photo"] . "</url>"; $writeTo .= "</link>"; } // End XML file $writeTo .= '</pages>'; $xmlfile = "links2.xml"; $file = fopen($xmlfile,"w+"); fwrite($file, "<xml>" . $writeTo . "</xml>"); fclose($file); echo "File was written."; ?> Quote Link to comment https://forums.phpfreaks.com/topic/152385-solved-save-as-xml-file/#findComment-800630 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.