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.
<?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() →
← Subscribe to our YouTube Channel here
This article is written by plus2net.com team.
https://www.plus2net.com
plus2net.com