PHP simplexml_load_string() Function

Definition and Usage

The simplexml_load_string() function loads an XML string into an object.

If the operation fails, it returns false.

Grammar

simplexml_load_file(string,class,options,ns,is_prefix)
Parameter Description
string Required. Specifies the XML string to be used.
class Optional. Specifies the class of the new object.
options Optional. Specifies additional Libxml parameters.
ns Optional.
is_prefix Optional.

Return Value

Returns an object of the class SimpleXMLElement, whose properties contain the data from the XML document. If it fails, it returns false.

Example

<?php
$xmlstring = <<<XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>
XML;
$xml = simplexml_load_string($xmlstring);
var_dump($xml);
?>

Output:

object(SimpleXMLElement)#1 (4)
{
["to"]=> string(4) "George"
["from"]=> string(4) "John"
["heading"]=> string(8) "Reminder"
["body"]=> string(29) "Don't forget the meeting!"
}