Skip to content

simplexml_load_string()

Introduction

SimpleXML is a PHP extension that provides a simple and easy-to-use API for working with XML documents. The simplexml_load_string() function is one of the many functions that SimpleXML provides to work with XML documents. It is a powerful tool that can be used to load an XML document from a string and create a SimpleXMLElement object. In this article, we will be discussing the simplexml_load_string() function in detail and how it can be used in PHP.

Understanding the simplexml_load_string() function

The simplexml_load_string() function in PHP loads an XML document from a string and creates a SimpleXMLElement object. (Note: This is the procedural approach; the object-oriented equivalent is new SimpleXMLElement($string).) The syntax for using the simplexml_load_string() function is as follows:

Syntax

php
simplexml_load_string ( string $data [, string $class_name = "SimpleXMLElement" [, int $options = 0 [, string $ns = "" [, bool $is_prefix = false ]]]] ) : SimpleXMLElement

Here, $data is the string containing the XML document to load. $class_name is an optional parameter that specifies the name of the class to use for the SimpleXMLElement object. $options is an optional parameter that specifies additional options for loading the XML document. $ns is an optional parameter that specifies the namespace to use for the XML document. $is_prefix is an optional parameter that specifies whether the namespace is a prefix.

Example Usage

Let's look at an example to understand the usage of the simplexml_load_string() function in PHP:

Example Usage of the simplexml_load_string() function in PHP

php
<?php

$xmlString = '<books><book><title>PHP Basics</title></book></books>';
$xml = simplexml_load_string($xmlString);
foreach($xml->book as $book) {
  echo $book->title . "\n";
}

In the example above, we first create a string containing an XML document. We then use the simplexml_load_string() function to load the XML document from the string and create a SimpleXMLElement object. We use a foreach loop to iterate over each book element in the XML document and print the title of each book.

Error Handling

By default, simplexml_load_string() will emit warnings if the XML is malformed. To handle errors gracefully, you can use libxml_use_internal_errors():

php
<?php

$xmlString = '<books><book><title>PHP Basics</title></books>'; // Missing closing </book> tag
libxml_use_internal_errors(true);
$xml = simplexml_load_string($xmlString);

if ($xml === false) {
  foreach (libxml_get_errors() as $error) {
    echo "Error: ", $error->message, "\n";
  }
} else {
  foreach($xml->book as $book) {
    echo $book->title . "\n";
  }
}

Conclusion

The simplexml_load_string() function is a powerful tool that can be used to load an XML document from a string and create a SimpleXMLElement object. It is an essential function to use when working with XML documents in PHP. By using the simplexml_load_string() function, developers can quickly and easily load XML documents from strings and create SimpleXMLElement objects using object-oriented syntax. We hope this article has provided you with a comprehensive overview of the simplexml_load_string() function in PHP and how it can be used. If you have any questions or need further assistance, please do not hesitate to ask.

Practice

What does the 'simplexml_load_string' function in PHP do according to https://www.w3docs.com/learn-php/simplexml-load-string.html?

Dual-run preview — compare with live Symfony routes.