PHP Simplexml_load_string () function

PHP

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 takes five parameters as shown in the above syntax. All of these options are described below:
  • $data : well-formed XML string.
  • $classname : class of the new object.
  • $options : Additional Libxml options are set by specifying an option and 1 or 0.
  • $ns : TRUE, if ns - prefix. FALSE if ns - URI. Default FALSE
  • $is_prefix : TRUE if ns - prefix. FALSE if ns - URI. FALSE by default
Possible values ​​of the $optionsparameter:
  • LIBXML_COMPACT: activate node allocation optimization .
  • LIBXML_DTDATTR: set default DTD attributes
  • LIBXML_DTDLOAD: load external subset
  • LIBXML_DTDVALID: validate with DTD
  • LIBXML_NOBLANKS: remove empty nodes
  • LIBXML_NOCDATA: merge CDATA as text nodes
  • LIBXML_NOEMPTYTAG: expand empty tags
  • LIBXML_NOENT: replacement objects
  • LIBXML_NOERROR: do not show error reports <
  • LIBXML_NONET: disable network access when loading documents
  • LIBXML_NOWARNING: do not show warnings
  • LIBXML_NOXMLDECL: remove XML declaration when saving document
  • LIBXML_NSCLEAN: remove redundant namespace declarations
  • LIBXML_PARSEHUGE: by setting sets the XML_PARSE_HUGE flag
  • LIBXML_XINCLUDE: implement XInclude substitution
  • LIBXML_ERR_ERROR: get recoverable errors
  • LIBXML_ERR_FATAL: get fatal errors
  • LIBXML_ERR_NONE: don't get 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.The following programs illustrate the simplexml_load_string() function: Program 1 : $note = < < < XML < note > < to > User 1 < / to > < from > User 2 < / from > 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 : $note = < < < XML "1.0" encoding = "ISO-8859 -1 " ? & Gt; < book > < name > PHP < / name > < name > Java < / name > < name > C++ < / name > < name > Python < / name > < / book >
XML; $xml = simplexml_load_string ( $note ); echo $xml -> getName(). "" ; foreach ( $xml -> children() as $child ) { echo $child -> getName(). ":" . $child . "" ; }
?>
Output:
book name: PHP name: Java name: C++ name: Python 
Link:
http://php.net/manual/en/function.simplexml-load-string.php