<!-- 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;
}
?> |