Generating XML file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
robbb99
Forum Newbie
Posts: 2
Joined: Fri Apr 03, 2009 8:21 am
Location: London

Generating XML file

Post 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. :roll:

Thanks for your help,
Rob
Reviresco
Forum Contributor
Posts: 172
Joined: Tue Feb 19, 2008 4:18 pm
Location: Milwaukee

Re: Generating XML file

Post by Reviresco »

To read an xml file, you can use simplexml:
http://us2.php.net/manual/en/function.s ... d-file.php

To write your xml to a file, you can use fwrite:
http://us2.php.net/manual/en/function.fwrite.php
robbb99
Forum Newbie
Posts: 2
Joined: Fri Apr 03, 2009 8:21 am
Location: London

Re: Generating XML file

Post 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?
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: Generating XML file

Post by php_east »

use browser's "view source" to view xml data.
Reviresco
Forum Contributor
Posts: 172
Joined: Tue Feb 19, 2008 4:18 pm
Location: Milwaukee

Re: Generating XML file

Post 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);
 
 
Post Reply