simplexml_load_file ( $xml_string,$class_name, $options, ns, is_prefix)
$xml_string : Required , formatted xml data string <?Php
header('Content-Type: text/xml');
$str_xml = simplexml_load_file("xml-sample2.xml");
$e = new SimpleXMLElement($str_xml->asXML());
echo $e->asXML()
?>
Output is here
<details>
<name>Abcd</name>
<address>
<door_no>234-ac</door_no>
<street>Great Wall</street>
<city>Alabama</city>
</address>
</details>
<?Php
//header('Content-Type: text/xml');
$str_xml = simplexml_load_file("xml-sample2.xml");
$e = new SimpleXMLElement($str_xml->asXML());
//echo $e->asXML()
echo $e->name; // Output is Abcd
echo "<br>";
echo $e->address->door_no; // Output is 234-ac
?>
Output is
Abcd
234-ac
<?Php
header('Content-Type: text/xml');
$str_xml = simplexml_load_file("file-xml-demo.xml");
$e = new SimpleXMLElement($str_xml->asXML());
echo $e->asXML();
?>
To list some of the node values
<?Php
//header('Content-Type: text/xml');
$str_xml = simplexml_load_file("file-xml-demo.xml");
$e = new SimpleXMLElement($str_xml->asXML());
//echo $e->asXML();
echo $e->details[1]->name; // output Max Ruin
echo "<br>";
echo $e->details[2]->class; // Output is Three
?>
Output is here
Max Ruin
Three
<?Php
//header('Content-Type: text/xml');
$str_xml = simplexml_load_file("file-xml-demo.xml");
$e = new SimpleXMLElement($str_xml->asXML());
foreach ($e as $child1) {
echo "<br>". $child1->getName()." : $child1";
foreach ($child1 as $child2) {
echo "<br>". $child2->getName()." : $child2";
}
}
?>
Output is here ( Part of the output is shown )
details :
id : 1
name : John Deo
class : Four
details :
id : 2
name : Max Ruin
class : Three
details :
id : 3
name : Arnold
class : Three
details :
id : 4
name : Krish Star
class : Four
details :
......
.....
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.