simplexml_load_string

We can create an object by using XML formatted string to an object by using simplexml_load_string function in PHP.
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.

xml-sample1.php

<?Php
require "xml-sample1.php";
$str=simplexml_load_string($str_xml);
print_r($str);
?>
The Output is here.
SimpleXMLElement Object ( [name] => Abcd [address] => SimpleXMLElement Object ( [door_no] => 234-ac [street] => Great Wall [city] => Alabama ) )

Example 2

<?Php
$str_xml = <<<XML
<?xml version='1.0' standalone='yes'?>
<details>
<name>Abcd</name>
<address>
  <door_no>234-ac</door_no>
  <street>Great Wall</street>
  <city>Alabama</city>
</address>
</details>
XML;
$str=simplexml_load_string($str_xml);
print_r($str);
?>
Output is here
SimpleXMLElement Object ( [name] => Abcd [address] => SimpleXMLElement Object ( [door_no] => 234-ac [street] => Great Wall [city] => Alabama ) )

Example 3

<?Php
$str_xml = <<<XML
<?xml version='1.0' standalone='yes'?>
<details>
<name>Abcd</name>
<address>
  <door_no>234-ac</door_no>
  <street>Great Wall</street>
  <city>Alabama</city>
</address>
</details>
XML;
$str=simplexml_load_string($str_xml);
echo $str->address->count(); // Output is 1 
echo "<br>";
echo $str->address[0]->count(); // Output is 3 
echo "<br>";
echo $str->address[0]->street; // Output is Great Wall 
echo "<br>";
echo $str->address->city; // Output is Alabama 
?>

XML XML count()

Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer