Pour réaliser un "tri" dans un code XML, on peut utiliser la méthode xpath :
CODE
<?php
$string = <<<XML
<a>
<b>
<c>text</c>
<c>stuff</c>
</b>
<d>
<c>code</c>
</d>
</a>
XML;
$xml = new SimpleXMLElement($string);
/* On cherche <a><b><c> */
$result = $xml->xpath('/a/b/c');
while(list( , $node) = each($result)) {
echo '/a/b/c: ',$node,"\n";
}
/* Les chemins relatifs fonctionnent aussi... */
$result = $xml->xpath('b/c');
while(list( , $node) = each($result)) {
echo 'b/c: ',$node,"\n";
}
?>
$string = <<<XML
<a>
<b>
<c>text</c>
<c>stuff</c>
</b>
<d>
<c>code</c>
</d>
</a>
XML;
$xml = new SimpleXMLElement($string);
/* On cherche <a><b><c> */
$result = $xml->xpath('/a/b/c');
while(list( , $node) = each($result)) {
echo '/a/b/c: ',$node,"\n";
}
/* Les chemins relatifs fonctionnent aussi... */
$result = $xml->xpath('b/c');
while(list( , $node) = each($result)) {
echo 'b/c: ',$node,"\n";
}
?>
J'aimerais, dans ce code, au lieu de mettre directement la portion de code XML, qu'on l'a récupère à partir d'un fichier distant (URL). J'ai essayé un $string = fopen ("http://www.domaine.com/fichier.xml"); mais rien de bien de convaincant. :/
Quelqu'un peut-il m'éclairer ?
Merci.
