PHP simplexml_load_string()Functions
PHP SimpleXML Reference Manual
Examples
Convert a well-formed XML string into a SimpleXMLElement object, then output the keys and elements of the object:
$note=<<<XML
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
XML;
$xml=simplexml_load_string($note);
print_r($xml);
Run Example »
Definition and Usage
The simplexml_load_string() function converts a well-formed XML string into a SimpleXMLElement object.
Syntax
simplexml_load_string(data,classname,options,ns,is_prefix);
| Parameters | Description |
|---|---|
| data | Required. Specifies a well-formed XML string. |
| classname | Optional. Specifies the class of the new object. |
| options | Optional. Specifies additional Libxml parameters. Set by specifying options as 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. Ifnsis a prefix, it is TRUE; ifnsis a URI, it is FALSE. The default is FALSE. |
Technical Details
| Return value: | Returns a SimpleXMLElement object on success, or FALSE on failure. |
|---|---|
| PHP version: | 5+ |
More Examples
Example 1
Output the data of each element in the XML string:
$note=<<<XML
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't 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;
Run Example »
Example 2
Output the element names and data of each child node in the XML string:
$note=<<<XML
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't 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>";
}
Run Example »
PHP SimpleXML Reference Manual Other Extensions