simplexml_load_string and simplexml_load_file are buggy and don't work well on XML with a complex markup. I tried both functions to parse some VALID XML SOAP responses and could not even get an error message. I had to use DOMDocument instead (worked like a charm).

Here is how we suppose to check simplexml for errors (if you are lucky to see errors, not just "false" returned from function)

libxml_use_internal_errors(true);
$xml=simplexml_load_string($myXMLData); //or simplexml_load_file

foreach( libxml_get_errors() as $error ) {

    print_r($error);

}
Answer from vitaly goji on Stack Overflow
🌐
PHP
php.net › manual › en › function.simplexml-load-string.php
PHP: simplexml_load_string - Manual
simplexml_load_string( string $data, ?string $class_name = SimpleXMLElement::class, int $options = 0, string $namespace_or_prefix = "", bool $is_prefix = false ): SimpleXMLElement|false
🌐
W3Schools
w3schools.com › php › func_simplexml_load_string.asp
PHP simplexml_load_string() Function
The simplexml_load_string() function converts a well-formed XML string into an object.
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-simplexml_load_string-function
PHP | simplexml_load_string() Function - GeeksforGeeks
July 11, 2025 - <?php $note=<<<XML <?xml version="1.0" encoding="ISO-8859-1"?> <book> <name>PHP</name> <name>Java</name> <name>C++</name> <name>Python</name> </book> XML; $xml=simplexml_load_string($note); echo $xml->getName() . "\n"; foreach($xml->children() as $child){ echo $child->getName() .
🌐
Tutorialspoint
tutorialspoint.com › php › php_function_simplexml_load_string.htm
PHP simplexml_load_string() Function
The simplexml_load_string() accepts an (well-formed) XML string as a parameter converts it into an object of the SimpleXMLElement class and returns it.
🌐
W3Schools
w3schools.com › php › php_xml_simplexml_read.asp
PHP SimpleXML Parser
The PHP simplexml_load_string() function is used to read XML data from a string.
🌐
SitePoint
sitepoint.com › blog › php › parsing xml with simplexml
PHP Master | Parsing XML With SimpleXML
November 7, 2024 - Parsing XML is crucial for consuming the full breadth of APIs available, as many web services still return data in XML format. PHP’s SimpleXML extension, introduced in PHP 5.0, simplifies this task. To parse XML using SimpleXML, load the XML using either simplexml_load_file() or simplexml_load_string().
🌐
OnlinePHP
onlinephp.io › simplexml-load-string › manual
simplexml_load_string - OnlinePHP.io Example
simplexml_load_string( string$data, [string|null$class_name = SimpleXMLElement::class], [int$options = 0], [string$namespace_or_prefix = ""], [bool$is_prefix = false] ): SimpleXMLElement|false
Find elsewhere
🌐
Reintech
reintech.io › blog › beginners-guide-php-simplexml-library-xml-parsing
A Beginner's Guide to the PHP SimpleXML Library for XML Parsing | Reintech media
April 21, 2023 - # Apache sudo systemctl restart apache2 # Nginx with PHP-FPM sudo systemctl restart php-fpm · SimpleXML provides two primary functions for loading XML content: simplexml_load_file() for external files and simplexml_load_string() for string data.
Top answer
1 of 1
4

You can simply iterate the SimpleXMLElement objects by accessing the children with -> operator:

$xml = <<<'XML'
<info>
    <form tableid="1">
        <town_id>
            <option value="5102">Moscow</option>
            <option value="2587">London</option>
            <option value="717">Madrid</option>
            <option value="2513">Paris</option>
            <option value="5071">Berlin</option>
        </town_id>
        <town_id>
            <option value="9343">XTown</option>
        </town_id>
    </form>
</info>
XML;

$items = simplexml_load_string($xml);

foreach ($items as $form) {
    foreach ($form->town_id as $town) {
        foreach ($town->option as $option) {
            $attr = $option->attributes();
            printf("#%d - %s\n", $attr['value'], $option);
        }
    }
}

Output

#5102 - Moscow
#2587 - London
#717 - Madrid
#2513 - Paris
#5071 - Berlin
#9343 - XTown

XPath

Alternatively, use xpath method:

$options = $items->xpath('form/town_id/option');
foreach ($options as $option) {
  $attr = $option->attributes();
  printf("#%d - %s\n", $attr['value'], $option);
}

In this example I used an XPath expression relative to $items (root element, in particular). Adjust the XPath according to your needs. For example, you can fetch all options in the document with //option. Or you might even want to iterate all elements under form having option children:

$containers = $items->xpath('form/*[option]');
foreach ($containers as $c) {
  switch ($c->getName()) {
    case 'town_id':    $label = 'Towns';     break;
    case 'country_id': $label = 'Countries'; break;
    default:
      // Skipping unknown element name
      continue;
  }

  printf("\n%s\n======\n", $label);
  foreach ($c->option as $option) {
    $attr = $option->attributes();
    printf("#%d - %s\n", $attr['value'], $option);
  }
}

Sample Output

Towns
======
#5102 - Moscow
#2587 - London
#717 - Madrid
#2513 - Paris
#5071 - Berlin

Towns
======
#9343 - XTown

Countries
======
#3456 - Russia
#4566 - China
🌐
GitHub
github.com › php › doc-en › blob › master › reference › simplexml › functions › simplexml-load-string.xml
doc-en/reference/simplexml/functions/simplexml-load-string.xml at master · php/doc-en
<?php · $string = <<<XML · <?xml version='1.0'?> · <document> <title>Forty What?</title> <from>Joe</from> <to>Jane</to> <body> I know that's the answer -- but what's the question? </body> </document> XML; · $xml = simplexml_load_string($string); ·
Author   php
🌐
Plus2Net
plus2net.com › php_tutorial › php-xml-loadstring.php
simplexml_load_string to get an object from a XML formatted string
simplexml_load_string ( $xml_string,$class_name, $options, ns, is_prefix) $xml_string : Required , formatted xml data string $class_name : Optional , return object of specified class $options : Optional , Additional Libxml parameters ns : Optional , Namespace prefix is_prefix : True or False based on ns ( prefix True , URI is false ) Here we will take our sample XML formatted string. <?Php require "xml-sample1.php"; $str=simplexml_load_string($str_xml); print_r($str); ?> The Output is here.