<!-- Fichier : fichier.xml -->
<?xml version="1.0" encoding="iso-8859-1"?>
<liste>
<logiciel>
<nom langue="US" systeme_exploitation="Win">Cooktop 2.200</nom>
<commentaire>
Un editeur XML, XSLT, XPath et DTD puissant et totalement gratuit.
</commentaire>
<editeur adresse="http://xmleverywhere.com/cooktop/">
XML Everywhere
</editeur>
<prix monnaie="$US">00.00</prix>
</logiciel>
<logiciel>
<nom langue="US" systeme_exploitation="Win">XML Spy 4.1</nom>
<commentaire>Un editeur XML desormais mature.</commentaire>
<editeur adresse="http://www.xmlspy.com/default.html">
Altova Inc.
</editeur>
<prix monnaie="$US">199,00</prix>
</logiciel>
<logiciel>
<nom langue="US" systeme_exploitation="Win">
XML Spy 4.1 B2B Server
</nom>
<commentaire>
La version 4 en version Business to business.
</commentaire>
<editeur adresse="http://www.xmlspy.com/default.html">
Altova Inc.
</editeur>
<prix monnaie="$US">1 999,00</prix>
</logiciel>
<logiciel>
<nom langue="US" systeme_exploitation="Win">XMLwriter v1.21</nom>
<commentaire>Permet de creer des documents XML.</commentaire>
<editeur adresse="http://xmlwriter.net/">Wattle Software</editeur>
<prix monnaie="$US">75,00</prix>
</logiciel>
</liste>
<?php
// Fichier : traitement.php
function contenu_textuel($noeud_parent)
{
$noeuds = $noeud_parent->children();
while($noeud = array_shift($noeuds))
{
if ($noeud->type == XML_TEXT_NODE)
{
$resultat = $noeud->content;
return $resultat;
}
}
}
function traitement_element($noeud_parent, $nom)
{
$noeuds = $noeud_parent->children();
while($noeud = array_shift($noeuds))
{
if ($noeud->name == $nom)
{
$resultat = contenu_textuel($noeud);
return $resultat;
}
}
}
function traitement_attribut($noeud_parent, $nom, $attribut)
{
$noeuds = $noeud_parent->children();
while($noeud = array_shift($noeuds))
{
if ($noeud->name == $nom)
{
$resultat = $noeud->getattr($attribut);
return $resultat;
}
}
}
$tab_elements = array("nom","commentaire","editeur","prix");
$tab_attributs = array("langue"=>0,
"systeme_exploitation"=>0,
"adresse"=>2,
"monnaie"=>3);
$xml_doc = xmldocfile("c:\chemin\fichier.xml")
or die("Impossible d'ouvrir le fichier XML !");
$element_racine = $xml_doc->root();
$noeuds_enfants = $element_racine->children();
foreach($noeuds_enfants as $noeud)
{
if($noeud->type == XML_TEXT_NODE) continue;
for($i = 0; $i < sizeof($tab_elements); $i++)
{
${$tab_elements[$i]} = traitement_element($noeud, $tab_elements[$i]);
}
foreach($tab_attributs as $cle=>$valeur)
{
$$cle = traitement_attribut($noeud, $tab_elements[$valeur], $cle);
}
echo "<h4>Logiciel : " . $nom
. "</h4>Langue : " . $langue
. "<br>Système d'exploitation : " . $systeme_exploitation
. "<br>Commentaire : " . $commentaire
. "<br>Editeur : " . $editeur
. "<br>Adresse : " . $adresse
. "<br>Prix : " . $prix . " " . $monnaie;
}
?> |
<!-- Fichier : formulaire.html -->
<html>
<body>
<form method="post" action="traitement.php">
<table border="0">
<tr>
<td>Nom</td>
<td><input type="text" name="e_nom" size="30"></td>
</tr>
<tr>
<td>Langue</td>
<td><input type="text" name="e_langue" size="3"></td>
</tr>
<tr>
<td>Système d'exploitation</td>
<td><input type="text" name="e_systeme_exploitation" size="5"></td>
</tr>
<tr>
<td>Commentaire</td>
<td><textarea name="e_commentaire" cols="20" rows="3"></textarea></td>
</tr>
<tr>
<td>Editeur</td>
<td><input type="text" name="e_editeur" size="20"></td>
</tr>
<tr>
<td>Adresse</td>
<td><input type="text" name="e_adresse" size="30"></td>
</tr>
<tr>
<td>Prix - Monnaie</td>
<td><input type="text" name="e_prix" size="5">
- <input type="text" name="e_monnaie" size="3"></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="soumettre" value="Envoyer"></td>
</tr>
</table>
</form>
</body>
</html>
<?php
// Fichier : traitement.php
function creation_noeud($noeud_parent, $nom, $valeur)
{
$noeud_parent->new_child($nom, $valeur);
return $noeud_parent->lastchild();
}
$tab_elements = array("nom","commentaire","editeur","prix");
$tab_attributs = array("langue"=>0,
"systeme_exploitation"=>0,
"adresse"=>2,
"monnaie"=>3);
$xml_doc = xmldocfile("c:\chemin\fichier.xml")
or die("Impossible d'ouvrir le fichier XML !");
$element_racine = $xml_doc->root();
$logiciel = creation_noeud($element_racine, "logiciel", "");
foreach($tab_elements as $element)
{
$nom_contenu = "e_" . $element;
${$element} = creation_noeud($logiciel, $element, ${$nom_contenu});
}
foreach($tab_attributs as $attribut=>$num_element)
{
$nom_contenu = "e_" . $attribut;
${$tab_elements[$num_element]}->set_attribute($attribut,
${$nom_contenu});
}
$chaine_XML = $xml_doc->dumpmem();
$id_fichier = fopen("fichier.xml", "w+");
rewind($id_fichier);
fwrite($id_fichier, $chaine_XML);
echo fread($id_fichier, filesize("fichier.xml"));
fclose($id_fichier);
?> |