PHP simplexml_load_string() İşlev
Örnek
Bir XML dizesini bir nesneye dönüştürün, ardından nesnenin anahtarlarını ve öğelerini çıktılayın:
<?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);
?>
Tanım ve Kullanım
simplexml_load_string() işlevi, iyi biçimlendirilmiş bir XML dizesini bir nesneye dönüştürür.
Sözdizimi
simplexml_load_string(data, class, options, ns, is_prefix)
Parametre Değerleri
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 |
Teknik detaylar
Geri dönüş değeri: | Başarılı bir SimpleXMLElement nesnesi. Başarısızlıkta YANLIŞ |
---|---|
PHP Sürümü: | 5+ |
Daha fazla örnek
Örnek
XML dizesindeki her öğeden veri çıktısı alın:
<?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;
?>
Örnek
XML dizesindeki her alt düğüm için öğenin adını ve verilerini çıktılayın:
<?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 Referansı