YouTip LogoYouTip

Func Simplexml Load String

## PHP Certification ( --- ## PHP simplexml_load_string() Function ### Definition and Usage The simplexml_load_string() function converts an XML string into an object. ### Syntax ``` simplexml_load_string(xml,class,options,ns,is_prefix) ``` ### Parameters | Parameter | Description | |-----------|-------------| | xml | Required. Specifies an XML string | | class | Optional. Specifies a class that the new object is cast to | | options | Optional. Specifies additional Libxml parameters | | ns | Optional. Specifies a namespace URI | | is_prefix | Optional. Specifies whether ns is a prefix | ### Technical Details | Return Value: | Returns a SimpleXMLElement object on success, FALSE on failure | |---------------|----------------------------------------------------------------| | PHP Version: | PHP 5.0+ | --- ### More Examples **Example 1** Output the node values and attributes of the $xml variable: ``` <?php $xmlstring = ' Tove Jani Reminder Don't forget me this weekend! '; $xml = simplexml_load_string($xmlstring); echo $xml->to . "
"; echo $xml->from . "
"; echo $xml->heading . "
"; echo $xml->body; ?> ``` Output: ``` Tove Jani Reminder Don't forget me this weekend! ``` **Example 2** Output the node values and attributes of the $xml variable using the children() function: ``` <?php $xmlstring = ' Tove Jani Reminder Don't forget me this weekend! '; $xml = simplexml_load_string($xmlstring); echo $xml->getName() . "
"; foreach($xml->children() as $child) { echo $child->getName() . ": " . $child . "
"; } ?> ``` Output: ``` note to: Tove from: Jani heading: Reminder body: Don't forget me this weekend! ```
← Func Simplexml XpathFunc Simplexml Load File β†’