Fonction xpath() PHP

Définition et utilisation

La fonction xpath() exécute une requête XPath sur le document XML.

Si succès, retourne un tableau contenant un objet SimpleXMLElement. Si échoue, retourne false.

Syntaxe

class SimpleXMLElement
{
string xpath(chemin)
}
Paramètres Description
chemin Obligatoire. Chemin XPath.

Exemple

Fichier XML :

<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>George</to>
<from>John</from>
<heading>Rappel</heading>
<body>Ne oubliez pas la réunion !</body>
</note>

Code PHP :

<?php
$xml = simplexml_load_file("test.xml");
$result = $xml->xpath("from");
print_r($result);
?>

Sortie :

Tableau
(
[0] => Objet SimpleXMLElement
  (
  [0] => John
  )
)