PHP simplexml_load_string() Funktion
Beispiel
Konvertieren Sie einen XML-String in ein Objekt und geben Sie dann Schlüssel und Elemente des Objekts aus:
<?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);
?>
Definition und Verwendung
Die Funktion simplexml_load_string() wandelt einen wohlgeformten XML-String in ein Objekt um.
Syntax
simplexml_load_string(data, class, options, ns, is_prefix)
Parameterwerte
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 |
Technische Details
Rückgabewert: | Bei Erfolg ein SimpleXMLElement-Objekt. FALSE bei Fehler |
---|---|
PHP-Version: | 5+ |
Mehr Beispiele
Beispiel
Geben Sie die Daten von jedem Element in der XML-Zeichenfolge aus:
<?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;
?>
Beispiel
Geben Sie den Namen und die Daten des Elements für jeden untergeordneten Knoten in der XML-Zeichenfolge aus:
<?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>";
}
?>
❮ PHP-SimpleXML-Referenz