PHP simplexml_load_string() Funzione
Esempio
Converti una stringa XML in un oggetto, quindi restituisce le chiavi e gli elementi dell'oggetto:
<?php
$note=<<<XML
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Do not forget me this weekend!</body>
</note>
XML;
$xml=simplexml_load_string($note);
print_r($xml);
?>
Definizione e utilizzo
La funzione simplexml_load_string() converte una stringa XML ben formattata in un oggetto.
Sintassi
simplexml_load_string(data, class, options, ns, is_prefix)
Valori dei parametri
Parameter | Description |
---|---|
data | Required. Specifies a well-formed XML string |
class | Optional. Specifies the class of the new object |
options | Optional. Specifies additional Libxml parameters. Is set by specifying the option and 1 or 0 (TRUE or FALSE, e.g. LIBXML_NOBLANKS(1)) Possible values:
|
ns | Optional. Specifies a namespace prefix or URI |
is_prefix | Optional. Specifies a Boolean value. TRUE if ns is a prefix. FALSE if ns is a URI. Default is FALSE |
Dettagli tecnici
Valore di ritorno: | Un oggetto SimpleXMLElement in caso di successo. FALSO in caso di fallimento |
---|---|
Versione PHP: | 5+ |
Altri esempi
Esempio
Emetti i dati da ciascun elemento nella stringa XML:
<?php
$note=<<<XML
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Do not forget me this weekend!</body>
</note>
XML;
$xml=simplexml_load_string($note);
echo $xml->to . "<br>";
echo $xml->from . "<br>";
echo $xml->heading . "<br>";
echo $xml->body;
?>
Esempio
Genera il nome e i dati dell'elemento per ogni nodo figlio nella stringa XML:
<?php
$note=<<<XML
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Do not forget me this weekend!</body>
</note>
XML;
$xml=simplexml_load_string($note);
echo $xml->getName() . "<br>";
foreach($xml->children() as $child)
{
echo $child->getName() . ": " . $child . "<br>";
}
?>
❮ Riferimento PHP SimpleXML