# PHP simplexml_load_string() Function

PHP SimpleXML Reference

## Example

Convert a well-formed XML string into a SimpleXMLElement object, then output 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);

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. Is set by specifying the option and 1 or 0 (TRUE or FALSE, e.g. LIBXML_NOBLANKS(1))

Possible values:

- LIBXML_COMPACT - Activate nodes allocation optimization (may speed up application)
- 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 (e.g. <br/> to <br></br>), only available in the DOMDocument->save() and DOMDocument->saveXML() functions
- 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, which relaxes any hardcoded limit from the parser. This affects limits like maximum depth of a document and limits of the size of text nodes
- 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 (e.g. 20605 or 20617)
- LIBXML_DOTTED_VERSION - Get 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. 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. FALSE on failure
PHP Version: | 5+

## More Examples

## Example 1

Output the data from 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's name and data for 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 Reference

### WEB HOSTING

UK Reseller Hosting

### WEB BUILDING

FREE Website BUILDER Free HTML5 Templates

### W3SCHOOLS EXAMS

HTML, CSS, JavaScript, PHP, jQuery, XML, and ASP Certifications

### SHARE THIS PAGE

### Color Picker

### HTML/CSS

HTML Tutorial HTML5 Tutorial CSS Tutorial CSS3 Tutorial Bootstrap Tutorial

### JavaScript

JavaScript Tutorial jQuery Tutorial jQuery Mobile Tutorial AngularJS Tutorial AJAX Tutorial JSON Tutorial

### Graphics

Canvas Tutorial SVG Tutorial Google Maps Tutorial

### Server Side

SQL Tutorial PHP Tutorial ASP Tutorial ASP.NET Tutorial VBScript Tutorial AppML Tutorial

### Web

Web Building Web Statistics Web Certification

### XML

XML Tutorial DTD Tutorial Schema Tutorial XML DOM Tutorial XPath Tutorial XSLT Tutorial XQuery Tutorial XSL-FO Tutorial RSS Tutorial WSDL Tutorial

### HTML/CSS

HTML Tag Reference HTML Event Reference HTML Color Reference CSS 1,2,3 Reference Bootstrap Reference

### JavaScript

JavaScript Reference HTML DOM Reference jQuery Reference jQuery Mobile Reference Google Maps Reference

### Server Side

PHP Reference SQL Reference ASP Reference VBScript Reference Razor Reference ASP.NET Reference

### XML

XML DOM Reference XSLT Reference XSL-FO Reference XPath Reference XQuery Reference Schema Reference RSS Reference SVG Reference

### HTML Charsets

HTML Character Sets HTML ASCII HTML ANSI HTML Windows-1252 HTML ISO-8859-1 HTML Symbols HTML UTF-8

### HTML/CSS

HTML Examples CSS Examples

### Browser Scripting

JavaScript Examples HTML DOM Examples jQuery Examples jQuery Mobile Examples AngularJS Examples AJAX Examples

### Server Scripting

PHP Examples ASP Examples VBScript Examples Razor Examples .NET Examples

### XML Languages

XML Examples DTD Examples XML DOM Examples XSL Examples XSLT Examples XPath Examples XQuery Examples Schema Examples RSS Examples SVG Examples