Easy Tutorial
❮ Func Cal Juliantojd Php Anonymous Classes ❯

PHP simplexml_load_string() Function

PHP SimpleXML Reference Manual

Example

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

<?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);
?>

Definition and Usage

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


Syntax

Parameter 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 nodes' optimized configuration (speeds up the application)<br> LIBXML_DTDATTR - Set default DTD attributes<br> LIBXML_DTDLOAD - Load external subsets<br> LIBXML_DTDVALID - Validate with the DTD<br> LIBXML_NOBLANKS - Remove blank nodes<br> LIBXML_NOCDATA - Merge CDATA as text nodes<br> LIBXML_NOEMPTYTAG - Expand empty tags (e.g., <br/> to <br></br>), only valid for DOMDocument->save() and DOMDocument->saveXML() functions<br> LIBXML_NOENT - Substitute entities<br> LIBXML_NOERROR - Do not show error reports<br> LIBXML_NONET - Disable network access while loading documents<br> LIBXML_NOWARNING - Do not show warning reports<br> LIBXML_NOXMLDECL - Drop the XML declaration when saving a document<br> LIBXML_NSCLEAN - Remove redundant namespace declarations<br> LIBXML_PARSEHUGE - Set the XML_PARSE_HUGE flag, which relaxes any hardcoded limit from the parser. This affects limits like maximum depth of the document and limits on the size of text nodes.<br> LIBXML_XINCLUDE - Use XInclude substitution<br> LIBXML_ERR_ERROR - Get recoverable errors<br> LIBXML_ERR_FATAL - Get fatal errors<br> LIBXML_ERR_NONE - Get no errors<br> LIBXML_ERR_WARNING - Get simple warnings<br> LIBXML_VERSION - Get the libxml version (e.g., 20605 or 20617)<br> 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. A boolean value. TRUE if ns is a prefix, FALSE if ns is a URI. 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 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;
?>

Example 2

Output the element name 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);
foreach ($xml->children() as $child) {
    echo "Child node name: " . $child->getName() . "<br>";
    echo "Child node data: " . $child . "<br>";
}
?>
<?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>";
}
?>
❮ Func Cal Juliantojd Php Anonymous Classes ❯