PHP_XML.pdf
(
283 KB
)
Pobierz
XML et PHP
2008-2009
Programmation Web - PHP et XML
1
Technique 1: gestion manuelle (Bohf!)
Idée =
XML en sortie : générer directement le fichier XML soit
sur la sortie standard (via echo), soit dans un fichier,
XML en entrée: lire le fichier XML à partir des
instructions de base de lecture d'un fichier.
Inconvénients =
Pas malléable,
Très adHoc,
Obligation d'utiliser de la programmation d'assez-bas
niveau.
2008-2009
Programmation Web - PHP et XML
2
Exemple 1:
<?php
header('Content-type:
text/xml');
echo '<?xml version="1.0" encoding="ISO-8859-1"?>';
$mat_dist=array("paris" => array("nice" => 900, "lille" => 300),
"nice" => array("paris" => 900, "tours" => 800, "lille" => 1200),
"tours" => array("nice" => 800, "lille" => 500),
"lille" => array("paris" => 300, "nice" => 1200, "tours" => 800));
echo "<distance_entre_ville>\n";
foreach ($mat_dist as $ville_origine => $destinations )
foreach ($mat_dist[$ville_origine] as $ville_arrivee => $distance){
$ville_origine =
htmlspecialchars($ville_origine);
$ville_arrivee = htmlspecialchars($ville_arrivee);
$distance = htmlspecialchars($distance);
echo " <liaison>\n";
echo " <origine>$ville_origine</origine>\n";
echo " <destination>$ville_arrivee</destination>\n";
echo " <distance>$distance</distance>\n";
echo " </liaison>\n";
htmlspecialchars(string)
}
encode en XML certains caractères
echo "</distance_entre_ville>\n";
qui sont des "méta" en HTML
?>
par exemple remplace < par >
2008-2009
Programmation Web - PHP et XML
3
Fichier XML créé avec exemple 1
<?xml version="1.0" encoding="ISO-8859-1"?>
<distance_entre_ville>
<liaison>
<origine>paris</origine>
<destination>nice</destination>
<distance>900</distance>
</liaison>
<liaison>
<origine>paris</origine>
<destination>lille</destination>
<distance>300</distance>
</liaison>
<liaison>
<origine>nice</origine>
<destination>paris</destination>
<distance>900</distance> </liaison>
........
</distance_entre_ville>
2008-2009
Programmation Web - PHP et XML
4
Exemple 2 : BD vers XML manuellement
<?php
$user = 'toto'; $password = 'secretoto'; $database = 'nosamisleschiens'; $table = 'chien';
$connect = mysql_connect('localhost',$user, $password) or die ("erreur de connexion");
mysql_select_db($database, $connect) or die ("erreur de connexion base");
$fic = fopen("rep/export.xml", "w");
fwrite($fic,
'<?xml version="1.0" encoding="ISO-8859-1"?>');
fwrite($fic,
'<!-- DB to XML -->');
fwrite($fic,
"<$table>");
$elements =array();
$result =
mysql_query("describe
$table"); // describe = donne nom de colonne
while ($row =
mysql_fetch_array($result))
$elements[] = $row[0];
$result =
mysql_query("select
* from $table");
$i = 0;
while ($row =
mysql_fetch_array($result))
{ $i++;
fwrite($fic,
"<row num=\"$i\">");
for ($j = 0; $j<count($elements) ; $j++)
fwrite($fic,
"<$elements[$j]>".htmlspecialchars($row[$j]) ."</$elements[$j]>")
;
fwrite($fic,
"</row>");
}
fwrite($fic,
"</$table>");
mysql_close();
fclose($fic);
?>
2008-2009
Programmation Web - PHP et XML
5
Plik z chomika:
musli_com
Inne pliki z tego folderu:
The Hackademy Prog - Apprendre A Programmer En PHP.pdf
(34947 KB)
Apress.Professional.PHP4.PROPER.eBook-LiB.chm
(12608 KB)
John.Wiley.and.Sons.PHP5.and.MySQL.Bible.Apr.2004.eBook-DDU.pdf
(16849 KB)
Manuel PHP FR.pdf
(11855 KB)
Développer une application avec PHP et MySQL.pdf
(4444 KB)
Inne foldery tego chomika:
Apache
Artisteer
Javascript Ebook Collection
Joomla
jQuery
Zgłoś jeśli
naruszono regulamin