دالة PHP simplexml_load_string ()
مثال
قم بتحويل سلسلة 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);
print_r($xml);
?>
التعريف والاستخدام
تقوم الدالة simplexml_load_string () بتحويل سلسلة XML جيدة التكوين إلى كائن.
بناء الجملة
simplexml_load_string(data, class, options, ns, is_prefix)
قيمه المعامل
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 |
تفاصيل تقنية
قيمة الإرجاع: | كائن SimpleXMLElement عند النجاح. خطأ في الفشل |
---|---|
إصدار PHP: | 5+ |
مزيد من الأمثلة
مثال
إخراج البيانات من كل عنصر في سلسلة 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;
?>
مثال
أخرج اسم العنصر وبياناته لكل عقدة فرعية في سلسلة 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>";
}
?>
❮ مرجع PHP SimpleXML