PHP simplexml_load_string() Functie
Voorbeeld
Converteer een XML-tekenreeks naar een object en voer vervolgens sleutels en elementen van het object uit:
<?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);
?>
Definitie en gebruik
De functie simplexml_load_string() converteert een goed gevormde XML-tekenreeks naar een object.
Syntaxis
simplexml_load_string(data, class, options, ns, is_prefix)
Parameterwaarden
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
Winstwaarde: | Een SimpleXMLElement-object op succes. ONWAAR bij fout |
---|---|
PHP-versie: | 5+ |
Meer voorbeelden
Voorbeeld
Voer de gegevens uit van elk element in de XML-tekenreeks:
<?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;
?>
Voorbeeld
Voer de naam en gegevens van het element uit voor elk onderliggend knooppunt in de XML-tekenreeks:
<?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-referentie