PHP | simplexml_load_string() Function - GeeksforGeeks

08 May, 2018
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$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$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
PHP-string
PHP
Web Technologies
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Insert Form Data into Database using PHP ?
How to convert array to string in PHP ?
How to Upload Image into Database and Display it using PHP ?
How to check whether an array is empty using PHP?
PHP | Converting string to Date and DateTime
Remove elements from a JavaScript Array
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?