PHP | simplexml_load_string() Function Last Updated : 08 May, 2018 Comments Improve Suggest changes Like Article Like Report Sometimes there is a need of parsing XML data in PHP. There are a handful of methods available to parse XML data. SimpleXML is one of them. Parsing an XML document means that navigating through the XML document and return the relevant pieces of information. Nowadays, a few APIs return data in JSON format but there are still a large number of websites which returns data in XML format. So we have to master in parsing an XML document if we want to feast on APIs available. PHP SimpleXML was introduced back in PHP 5.0. The simplexml_load_string() function in PHP is used to interpret an XML string into an object. Syntax: simplexml_load_string($data, $classname, $options, $ns, $is_prefix); Parameters: This function accepts five parameters as shown in the above syntax. All of these parameters are described below: $data : A well-formed XML string. $classname : Class of the new object. $options : Additional Libxml parameters is set by specifying the option and 1 or 0. $ns : TRUE if ns is a prefix. FALSE if ns is a URI. Default is FALSE $is_prefix : TRUE if ns is a prefix. FALSE if ns is a URI. Default is FALSE Possible Values for the parameter $options are as follows: LIBXML_COMPACT : Activate nodes allocation optimization. LIBXML_DTDATTR : Set default DTD attributes LIBXML_DTDLOAD : Load external subset LIBXML_DTDVALID : Validate with the DTD LIBXML_NOBLANKS : Remove blank nodes LIBXML_NOCDATA : Merge CDATA as text nodes LIBXML_NOEMPTYTAG : Expand empty tags LIBXML_NOENT : Substitute entities LIBXML_NOERROR : Do not show error reports< LIBXML_NONET : Disable network access while loading documents LIBXML_NOWARNING : Do not show warning reports LIBXML_NOXMLDECL : Drop the XML declaration when saving a document LIBXML_NSCLEAN : Remove redundant namespace declarations LIBXML_PARSEHUGE : Sets XML_PARSE_HUGE flag LIBXML_XINCLUDE : Implement XInclude substitution LIBXML_ERR_ERROR : Get recoverable errors LIBXML_ERR_FATAL : Get fatal errors LIBXML_ERR_NONE : Get no errors LIBXML_ERR_WARNING : Get simple warnings LIBXML_VERSION : Get libxml version LIBXML_DOTTED_VERSION : Get dotted libxml version Return Value This function returns a SimpleXMLElement object on success and FALSE on failure. Below programs illustrate the simplexml_load_string() function: Program 1: php <?php $note=<<<XML <note> <to>User 1</to> <from>User 2</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; ?> Output: User 1 User 2 Reminder Don't forget me this weekend! Program 2: php <?php $note=<<<XML <?xml version="1.0" encoding="ISO-8859-1"?> <book> <name>PHP</name> <name>Java</name> <name>C++</name> <name>Python</name> </book> XML; $xml=simplexml_load_string($note); echo $xml->getName() . "\n"; foreach($xml->children() as $child){ echo $child->getName() . ": " . $child . "\n"; } ?> Output: book name : PHP name : Java name : C++ name : Python Reference : http://php.net/manual/en/function.simplexml-load-string.php Comment More infoAdvertise with us Next Article PHP | simplexml_load_string() Function S Saptarshi_Das Follow Improve Article Tags : Web Technologies PHP Similar Reads PHP | simplexml_load_file() Function The simplexml_load_file() function is an inbuilt function in PHP which is used to convert the well-formed XML document into the given file to an object. Syntax: SimpleXMLElement simplexml_load_file( string $filename, string $class_name = "SimpleXMLElement", int $options = 0, string $ns = "", bool $i 2 min read PHP | SimpleXMLElement __toString() Function The XMLReader::__toString() function is an inbuilt function in PHP which is used to get the text content that is directly in current element. This function does not return text content that is inside this element's children. Syntax:Â void XMLReader::__toString( void ) Parameters: This function doesn 1 min read PHP | SimpleXMLIterator key() Function The SimpleXMLIterator::key() function is an inbuilt function in PHP which is used to return the key of current element. Syntax: mixed SimpleXMLIterator::key( void ) Parameters: This function does not accept any parameters. Return Value: This function returns the XML tag name of the element SimpleXML 1 min read PHP | simplexml_import_dom() Function The simplexml_import_dom() function is an inbuilt function in PHP which is used to take a node of DOM document and convert it into a SimpleXML node. Syntax: SimpleXMLElement simplexml_import_dom( $node, $class_name = "SimpleXMLElement" ) Parameters: This function accepts two parameters as mentioned 1 min read PHP | SimpleXMLIterator next() Function The SimpleXMLIterator::next() function is an inbuilt function in PHP which is used to move the SimpleXMLIterator element to the next element. Syntax: void SimpleXMLIterator::next( void ) Parameters: This function does not accept any parameters. Return Value: This function does not return any value. 1 min read PHP | SimpleXMLIterator getChildren() Function The SimpleXMLIterator::getChildren() function is an inbuilt function in PHP which is used to return the SimpleXMLIterator object containing sub-elements of the current element. Syntax: SimpleXMLIterator SimpleXMLIterator::getChildren( void ) Parameters: This function does not accepts any parameters. 1 min read PHP | SimpleXMLIterator valid() Function The SimpleXMLIterator::valid() function is an inbuilt function in PHP which is used to check the current element is valid or not. Syntax: bool SimpleXMLIterator::valid( void ) Parameters: This function does not accept any parameters. Return Value: This function returns TRUE if the current element is 1 min read PHP | SimpleXMLIterator hasChildren() Function The SimpleXMLIterator::hasChildren() function is an inbuilt function in PHP which is used to check whether the current SimpleXMLIterator element has sub-elements or not. Syntax: bool SimpleXMLIterator::hasChildren( void ) Parameters: This function does not accept any parameters. Return Value: This f 1 min read PHP | SimpleXMLElement::getName() Function Pre-requisite: Read XML basicsThe SimpleXMLElement::getName() function is an inbuilt function in PHP which returns the name of the xml element.Syntax:Â Â string SimpleXMLElement::getName( void ) Parameter: This function does not accept any parameter.Return Value: It returns a string which represents 2 min read PHP | SimpleXMLElement XPath() Function Pre-requisite: Read XML Basics The SimpleXMLElement::xpath() function is an inbuilt function in PHP which runs XPath query on the XML document. Syntax: SimpleXMLElement::xpath( $path ) Parameters: This function accepts single parameter $path which is required. It is used to specify the XPath path of 2 min read Like