Open In App
Related Articles

PHP | simplexml_load_string() Function

Improve Article
Save Article
Like Article

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.


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_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:

  <to>User 1</to>
  <from>User 2</from>
  <body>Don't forget me this weekend!</body>
$xml = simplexml_load_string($note);
echo $xml->to . "<br>";
echo $xml->from . "<br>";
echo $xml->heading . "<br>";
echo $xml->body;


User 1
User 2
Don't forget me this weekend!

Program 2:

<?xml version="1.0" encoding="ISO-8859-1"?>
echo $xml->getName() . "\n";
foreach($xml->children() as $child){
   echo $child->getName() . ": " . $child . "\n";


name : PHP
name : Java
name : C++
name : Python

Reference :

Last Updated : 08 May, 2018
Like Article
Save Article
Similar Reads
Related Tutorials