PHP simplexml_load_file() function

Definition and Usage

The simplexml_load_file() function loads an XML document into an object.

If the operation fails, it returns false.

syntax

simplexml_load_file(file,class,options,ns,is_prefix)
parameters description
file Obligatoire. Définit le document XML à utiliser.
class Optionnel. Définit la classe du nouveau objet.
options Optionnel. Définit les paramètres supplémentaires de Libxml.
ns Optionnel.
is_prefix Optionnel.

Retour de valeur

Retourne un objet de la classe SimpleXMLElement, whose properties contain the data of the XML document. If it fails, it returns false.

Exemple

Fichier XML :

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

Code PHP :

<?php
if (file_exists('test.xml'))
  {
  $xml = simplexml_load_file('test.xml');
  var_dump($xml);
  }
else
  {
  exit('Erreur.');
  }
?>

Sortie :

object(SimpleXMLElement)#1 (4)
{
["to"]=> string(4) "George"
["from"]=> string(4) "John"
["heading"]=> string(8) "Rappel"
["body"]=> string(29) "Ne pas oublier la réunion!"
}