PHP simplexml_load_string() Function

In this chapter you will learn:

  1. Description for PHP simplexml_load_string() Function
  2. Syntax for PHP simplexml_load_string() Function
  3. Parameter for PHP simplexml_load_string() Function
  4. Return for PHP simplexml_load_string() Function
  5. Example - Convert a well-formed XML string into a SimpleXMLElement object
  6. Example - Output the data from each element in the XML string
  7. Example - Output the element's name and data for each child node in the XML string

Description

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

Syntax

Syntax for PHP simplexml_load_string() Function has the following syntax.

simplexml_load_string(data,classname,options,ns,is_prefix);

Parameter

ParameterIs requiredDescription
dataRequired.A well-formed XML string
classnameOptional.Class of the new object
optionsOptional.Additional Libxml parameters. Is set by specifying the option and 1 or 0 (TRUE or FALSE, e.g. LIBXML_NOBLANKS(1))
nsOptional.Namespace prefix or URI
is_prefixOptional.TRUE if ns is a prefix. FALSE if ns is a URI. Default is FALSE

Possible values for options:

  • 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
  • LIBXML_DOTTED_VERSION - Get dotted libxml version

Return

Returns a SimpleXMLElement object on success. FALSE on failure.

Example 1

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


<?php/*j  a v a 2s .  co  m*/
$note=<<<XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<book>
    <name>PHP</name>
    <name>Java</name>
</book>
XML;

$xml=simplexml_load_string($note);
print_r($xml);
?>

The code above generates the following result.

Example 2

Output the data from each element in the XML string:


<?php/*from  ja  v  a  2  s  .com*/
$note=<<<XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<book>
    <name>PHP</name>
    <name>Java</name>
</book>
XML;

$xml=simplexml_load_string($note);
echo $xml->to . "\n";
echo $xml->from . "\n";
echo $xml->heading . "\n";
echo $xml->body;
?>

Example 3

Output the element's name and data for each child node in the XML string:


<?php/*from  ja v  a2s.co  m*/
$note=<<<XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<book>
    <name>PHP</name>
    <name>Java</name>
</book>
XML;

$xml=simplexml_load_string($note);
echo $xml->getName() . "\n";

foreach($xml->children() as $child){
   echo $child->getName() . ": " . $child . "\n";
}
?>

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. Description for PHP xpath() Function
  2. Syntax for PHP xpath() Function
  3. Parameter for PHP xpath() Function
  4. Return for PHP xpath() Function
  5. Example - runs an XPath query on the XML document
Home » PHP Tutorial » PHP SimpleXML Functions
PHP SimpleXMLElement Create
PHP addAttribute() Function
PHP addChild() Function
PHP asXML() Function
PHP attributes() Function
PHP children() Function
PHP SimpleXML count() Function
PHP getDocNamespaces() Function
PHP getName() Function
PHP getNamespaces() Function
PHP registerXPathNamespace() Function
PHP simplexml_import_dom() Function
PHP simplexml_load_file() Function
PHP simplexml_load_string() Function
PHP xpath() Function