PHP simplexml_load_string()Functions

PHP SimpleXML 参考手册PHP SimpleXML Reference Manual

Examples

Convert a well-formed XML string into a SimpleXMLElement object, then output the keys and elements of the object:

<?php $note=<<<XML <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> 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 into 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 options as 1 or 0 (TRUE or FALSE, e.g., LIBXML_NOBLANKS(1)).

Possible values:

  • LIBXML_COMPACT - Activate node optimization configuration (can speed up applications)
  • LIBXML_DTDATTR - Set default DTD attributes
  • LIBXML_DTDLOAD - Load additional subset
  • LIBXML_DTDVALID - Validate DTD validity
  • LIBXML_NOBLANKS - Remove blank nodes
  • LIBXML_NOCDATA - Set CDATA as text nodes
  • LIBXML_NOEMPTYTAG - Expand empty tags (e.g., <br/> to <br></br>), only valid in DOMDocument->save() and DOMDocument->saveXML() functions
  • LIBXML_NOENT - Substitute entities
  • LIBXML_NOERROR - Do not display error reports
  • LIBXML_NONET - Stop accessing the network when loading documents
  • LIBXML_NOWARNING - Do not display warning reports
  • LIBXML_NOXMLDECL - Discard XML declaration when storing 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 will affect things such as the maximum depth of the document 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 - Do not get 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 value. Ifnsis a prefix, it is TRUE; ifnsis a URI, it is FALSE. The 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:

<?php $note=<<<XML <note> <to>Tove</to> <from>Jani</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; ?>

Run Example »

Example 2

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

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

Run Example »


PHP SimpleXML 参考手册PHP SimpleXML Reference Manual Other Extensions