Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PHP simplexml_load_string() Function

PHP simplexml_load_string() Function

PHP SimpleXML Reference Manual PHP SimpleXML Reference Manual

Example

Convert a well-formed XML string to a SimpleXMLElement object, then output the object's keys and elements:

Tove Jani Reminder Don't forget me this weekend! XML; $xml = simplexml_load_string ( $note ) ; print_r ( $xml ) ; ?>

Run Example »


Definition and Usage

The simplexml_load_string() function converts a well-formed XML string to a SimpleXMLElement object.


Syntax

simplexml_load_string(

data,classname,options,ns,is_prefix

);

Parameters Description
data Required. Specifies a well-formed XML string.
classname Optional. Specifies the class of the new object.
options Optional. Specifies additional Libxml parameters. Set by specifying the option as 1 or 0 (TRUE or FALSE, e.g. LIBXML_NOBLANKS(1)).

Possible values:

  • LIBXML_COMPACT - Activate optimized node configuration (can speed up the application)
  • LIBXML_DTDATTR - Set default DTD attributes
  • LIBXML_DTDLOAD - Load the external subset
  • LIBXML_DTDVALID - Validate DTD validity
  • LIBXML_NOBLANKS - Remove empty nodes
  • LIBXML_NOCDATA - Set CDATA as text nodes
  • LIBXML_NOEMPTYTAG - Expand empty tags (e.g.
    to

    ), only valid in the DOMDocument->save() and DOMDocument->saveXML() functions
  • LIBXML_NOENT - Substitute entities
  • LIBXML_NOERROR - Do not show error reports
  • LIBXML_NONET - Stop network access when loading the document
  • 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 - Set the XML_PARSE_HUGE flag to relax any mandatory limits of the parser. This affects things such as the maximum document depth and text node size limits.
  • LIBXML_XINCLUDE - Use 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 the libxml version (e.g. 20605 or 20617)
  • LIBXML_DOTTED_VERSION - Get the dotted libxml version (e.g. 2.6.5 or 2.6.17)
  • | | *ns* | Optional. Specifies a namespace prefix or URI. | | *is_prefix* | Optional. Specifies a boolean. If *ns* is a prefix then TRUE, if *ns* is a URI then FALSE. Default is FALSE. |

    Technical Details

    Return Value: Returns a SimpleXMLElement object on success, or FALSE on failure.
    PHP Version: 5+

    More examples

    Example 1

    Output the data of each element in the XML string:

    Tove Jani Reminder Don't forget me this weekend! XML; $xml = simplexml_load_string ( $note ) ; echo $xml -> to . "
    " ; echo $xml -> from . "
    " ; echo $xml -> heading . "
    " ; echo $xml -> body ; ?>

    Run Example »

    Example 2

    Output the element names and data of each child node in the XML string:

    Tove Jani Reminder Don't forget me this weekend! XML; $xml = simplexml_load_string ( $note ) ; echo $xml -> getName ( ) . "
    " ; foreach ( $xml -> children ( ) as $child ) { echo $child -> getName ( ) . " : " . $child . "
    " ; } ?>

    Run Example »


    PHP SimpleXML Reference Manual PHP SimpleXML Reference Manual