PHP simplexml_load_string() Funksie
Voorbeeld
Omskep 'n XML-string in 'n voorwerp, voer dan sleutels en elemente van die voorwerp 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);
?>
Definisie en gebruik
Die simplexml_load_string() funksie omskep 'n goed gevormde XML-string in 'n objek.
Sintaksis
simplexml_load_string(data, class, options, ns, is_prefix)
Parameterwaardes
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 |
Tegniese besonderhede
Terugkeerwaarde: | 'n SimpleXMLElement-objek oor sukses. ONWAAR op mislukking |
---|---|
PHP weergawe: | 5+ |
Meer voorbeelde
Voorbeeld
Voer die data van elke element in die XML-string 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);
echo $xml->to . "<br>";
echo $xml->from . "<br>";
echo $xml->heading . "<br>";
echo $xml->body;
?>
Voorbeeld
Voer die element se naam en data uit vir elke kindernodus in die XML-string:
<?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-verwysing