children() function in PHP XML

From a XML string or tag we can get the children of a given node. We will be using simpleXMLElement object for this. Here is the XML string.

xml-sample1.php

<?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;
?>

We have also used getName to display the tag name along with the children function. Here is the php code.

<?php
require "xml-sample1.php";
$main1 = new SimpleXMLElement($str_xml);

foreach ($main1->children() as $child1) {
echo '<br><b>getName</b> : '.$child1->getName().' <b>children</b> : '. $child1;
foreach ($child1->children() as $child2) {
echo '<br><b>getName</b> : '.$child2->getName().' <b>children</b> : '. $child2;
}
}
?>

The output is

getName : name children : Abcd
getName : address children :
getName : door_no children : 234-ac
getName : street children : Great Wall
getName : city children : Alabama

Your Rating




Google+

Post Comment This is for short comments only. Use the forum for more discussions.




HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us
©2000-2018 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer