simplexml_load_string

(PHP 5)

simplexml_load_string -- Convertit un chaîne XML en objet

Description

object simplexml_load_string ( string data [, string class_name [, int options]] )

simplexml_load_string() convertit la chaîne XML data et retourne un objet de la classe SimpleXMLElement possédant des propriétés contenant les données de la chaîne. En cas d'erreurs, cette fonction retourne FALSE.

Vous pouvez utiliser le paramètre optionnel class_name et ainsi, la fonction simplexml_load_string() retournera un objet de la classe spécifiée. Cette classe doit étendre la classe SimpleXMLElement.

Depuis PHP 5.1.0 et Libxml 2.6.0, vous pouvez aussi utiliser le paramètre options pour spécifier des paramètres additionnels Libxml.

Exemple 1. Convertir une chaîne XML

<?php
$string
= <<<XML
<?xml version='1.0'?>
<document>
<title>Forty What?</title>
<from>Joe</from>
<to>Jane</to>
<body>
  I know that's the answer -- but what's the question?
</body>
</document>
XML;

$xml = simplexml_load_string($string);

var_dump($xml);
?>

L'exemple ci-dessus va afficher :

SimpleXMLElement Object
(
  [title] => Forty What?
  [from] => Joe
  [to] => Jane
  [body] =>
   I know that's the answer -- but what's the question?
)

A partir de là, vous pouvez utiliser $xml->body et tout autre élément.

Voir aussi simplexml_load_file().