<?php
$source = <<<XML
<?xml version="1.0" standalone="yes" 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>
XML;
?>
<html>
<body>
<?php
$xml = simplexml_load_string($source);
var_dump($xml);
/*Affiche :
object(SimpleXMLElement)#1 (1) {
["logiciel"]=> array(4) {
[0]=> object(SimpleXMLElement)#2 (4) {
["nom"]=> string(13) "Cooktop 2.200"
["commentaire"]=> string(66) "Un editeur XML, XSLT, XPath et
DTD puissant et totalement gratuit."
["editeur"]=> string(14) "XML Everywhere"
["prix"]=> string(5) "00.00"
}
[1]=> object(SimpleXMLElement)#3 (4) {
["nom"]=> string(11) "XML Spy 4.1"
["commentaire"]=> string(32) "Un editeur XML desormais mature."
["editeur"]=> string(11) "Altova Inc."
["prix"]=> string(6) "199,00"
}
[2]=> object(SimpleXMLElement)#4 (4) {
["nom"]=> string(22) "XML Spy 4.1 B2B Server"
["commentaire"]=> string(45) "La version 4 en version Business to business."
["editeur"]=> string(11) "Altova Inc."
["prix"]=> string(8) "1 999,00"
}
[3]=> object(SimpleXMLElement)#5 (4) {
["nom"]=> string(15) "XMLwriter v1.21"
["commentaire"]=> string(34) "Permet de creer des documents XML."
["editeur"]=> string(15) "Wattle Software"
["prix"]=> string(5) "75,00"
}
}
}
*/
//Modification de la valeur d'un élément
$xml->logiciel[0]->nom = 'Cooktop';
var_dump($xml);
/*Affiche :
object(SimpleXMLElement)#1 (1) {
["logiciel"]=> array(4) {
[0]=> object(SimpleXMLElement)#2 (4) {
["nom"]=> string(13) "Cooktop"
["commentaire"]=> string(66) "Un editeur XML, XSLT, XPath et
DTD puissant et totalement gratuit."
["editeur"]=> string(14) "XML Everywhere"
["prix"]=> string(5) "00.00"
}
[1]=> object(SimpleXMLElement)#3 (4) {
["nom"]=> string(11) "XML Spy 4.1"
["commentaire"]=> string(32) "Un editeur XML desormais mature."
["editeur"]=> string(11) "Altova Inc."
["prix"]=> string(6) "199,00"
}
[2]=> object(SimpleXMLElement)#4 (4) {
["nom"]=> string(22) "XML Spy 4.1 B2B Server"
["commentaire"]=> string(45) "La version 4 en version Business to business."
["editeur"]=> string(11) "Altova Inc."
["prix"]=> string(8) "1 999,00"
}
[3]=> object(SimpleXMLElement)#5 (4) {
["nom"]=> string(15) "XMLwriter v1.21"
["commentaire"]=> string(34) "Permet de creer des documents XML."
["editeur"]=> string(15) "Wattle Software"
["prix"]=> string(5) "75,00"
}
}
}
*/
//Sauvegarde des modifications
$contenu = $xml->asXML();
$res = fopen('fichier_sauv.xml', 'x+');
fwrite($res, $contenu)
fclose($res);
//Utilisation de XPath
var_dump($xml->xpath('/liste/logiciel/nom'));
/*Affiche :
array(4) {
[0]=> object(SimpleXMLElement)#7 (1) {
[0]=> string(7) "Cooktop"
}
[1]=> object(SimpleXMLElement)#6 (1) {
[0]=> string(11) "XML Spy 4.1"
}
[2]=> object(SimpleXMLElement)#8 (1) {
[0]=> string(22) "XML Spy 4.1 B2B Server"
}
[3]=> object(SimpleXMLElement)#9 (1) {
[0]=> string(15) "XMLwriter v1.21"
}
}
$element = $xml->xpath('/liste/logiciel[2]/editeur');
var_dump($element);
//Affiche :
array(1) {
[0]=> object(SimpleXMLElement)#7 (1) { [0]=> string(11) "Altova Inc." }
}
//Accès aux attributs d'un élément XML
foreach($element[0]->attributes() as $nom => $valeur){
echo '<p>' . $nom . ' = ' . $valeur . '</p>';
}
//Affiche :
adresse = http://www.xmlspy.com/default.html
//Chargement d'un fichier XML
$sxml = simplexml_load_file('fichier.xml');
parcourir($sxml);
function parcourir($sxml){
$i = 0;
//Accès aux enfants de l'élément courant
foreach($sxml->children() as $element){
echo $i++ . ' ';
var_dump($element);
echo '<br>';
parcourir($element);
}
echo '<br>';
}
/*Affiche :
0 object(SimpleXMLElement)#4 (4) { ... }
0 object(SimpleXMLElement)#7 (1)
{ [0]=> string(13) "Cooktop 2.200" }
1 object(SimpleXMLElement)#9 (1)
{ [0]=> string(66) "Un editeur XML, XSLT, XPath et
DTD puissant et totalement gratuit." }
2 object(SimpleXMLElement)#8 (1)
{ [0]=> string(14) "XML Everywhere" }
3 object(SimpleXMLElement)#7 (1)
{ [0]=> string(5) "00.00" }
1 object(SimpleXMLElement)#7 (4) { ... }
0 object(SimpleXMLElement)#5 (1)
{ [0]=> string(11) "XML Spy 4.1" }
1 object(SimpleXMLElement)#8 (1)
{ [0]=> string(32) "Un editeur XML desormais mature." }
2 object(SimpleXMLElement)#9 (1)
{ [0]=> string(11) "Altova Inc." }
3 object(SimpleXMLElement)#5 (1)
{ [0]=> string(6) "199,00" }
2 object(SimpleXMLElement)#5 (4) { ... }
0 object(SimpleXMLElement)#4 (1)
{ [0]=> string(22) "XML Spy 4.1 B2B Server" }
1 object(SimpleXMLElement)#9 (1)
{ [0]=> string(45) "La version 4 en version Business to business." }
2 object(SimpleXMLElement)#8 (1)
{ [0]=> string(11) "Altova Inc." }
3 object(SimpleXMLElement)#4 (1)
{ [0]=> string(8) "1 999,00" }
3 object(SimpleXMLElement)#4 (4) { ... }
0 object(SimpleXMLElement)#7 (1)
{ [0]=> string(15) "XMLwriter v1.21" }
1 object(SimpleXMLElement)#8 (1)
{ [0]=> string(34) "Permet de creer des documents XML." }
2 object(SimpleXMLElement)#9 (1)
{ [0]=> string(15) "Wattle Software" }
3 object(SimpleXMLElement)#7 (1)
{ [0]=> string(5) "75,00" }
*/
?>
</body>
</html> |