Click to See Complete Forum and Search --> : How to create an xml in a php file (not with a php file, but in it)


Ness_du_Frat
03-14-2010, 07:47 AM
Hi !
For my flash site, I need to create a php file which would generate an xml file, so that if I call "myphpfile.php", it gives me this file, but with the xml inside.
I've tried different ways, and they all give me an error. I can create an xml with a php file in a separate file allright, but that's not the point, since the xml will be changing very often and I need the mySQL database, as I'll have an html website for people who don't want to get the flash player.
I need to call the php file from flash, so the xml needs to "replace" the php file, but still be able to change if the db changes.
So, I'll put the code I used :


<?
header ("Content-Type:text/xml");
// connection stuff
mysql_query("SET NAMES 'utf8'");
$connection = mysql_connect("$hostname" , "$user" , "$pass");
$db = mysql_select_db($dbase , $connection);
$query='SELECT * FROM persos';
$results = mysql_query($query, $connection) or die("Impossible d'interroger la base de données");
$num = mysql_num_rows($result);

?><?php */?>
<?
echo "<?XML version=\"1.0\" encoding=\"UTF-8\"?>\n";
echo "<content>\n";

while($row=mysql_fetch_assoc($results)){
echo "<perso titre=\"".$row["title"]."\">\n";
echo "<nom>".$row["nom"]."</nom>\n";
echo "<physique>".$row["physique"]."</physique>\n";
echo "</perso>\n";
}
echo "</content>\n";
mysql_close($connection);
?>


This is the file I get : http://zarkan.org/test/persos.php with an error.

If I don't force the header with xml, I get an html file with xml tags in the source. I need the xml to actually appear in the page, like with any other xml page.

The only way that worked is :

<?
if ($results) {
$doc = domxml_new_doc('1.0');
$node = $doc->create_element('content');
$carnet = $doc->append_child($node);

while ($row = mysql_fetch_array($results)) {
$node = $doc->create_element('pagetitle');
$personne = $carnet->append_child($node);

$node = $doc->create_element('nom');
$tmpNode = $personne->append_child($node);
$value = $doc->create_text_node($row['nom']);
$tmpNode->append_child($value);

$node = $doc->create_element('prenom');
$tmpNode = $personne->append_child($node);

$value = $doc->create_text_node($row['prenom']);
$tmpNode->append_child($value);

$node = $doc->create_element('physique');
$tmpNode = $personne->append_child($node);
$value = $doc->create_text_node($row['physique']);
$tmpNode->append_child($value);
}

$doc->dump_file('carnetMysqlToXmlWithDom.xml');
echo 'Export XML effectue !<br><a href="carnetMysqlToXmlWithDom.xml">Voir le fichier</a>';
}
?>

But I would need the xml to be in the current page, and not saved in a xml file. I really don't know how to do that, I've already spent hours on that...
Thanks for all advice !