Hi i am using DOM for retrieving the data from an xml file. The code below is working perfectly fine for the xml file, but the main problem i am facing is that it recognizes only the root nodes, it does not recognizes child nodes.
Here’s my code:-
$dom = new DOMDocument();
$dom->load($url);
$link = $dom->getElementsByTagName($tag_name);
$value = array();
for ($i = 0; $i < $link->length; $i++) {
$childnode['name']=$link->item($i)->nodeName;
$childnode['value']=$link->item($i)->nodeValue;
$value[ $childnode['name']] = $childnode['value'];
// echo $link->item($i)->nodeValue . '<br>';
$k++;
}
This is my view file where i am displaying the data
foreach($value as $node=>$value)
{
echo "<b> Node :</b>".$node."<br /><b>Value:</b>".$value."<br /><hr>";
}
This is my xml file
<name>John</name>
<place>Australia</place>
<contact>
<phone>8734563485</phone>
<type>Mobile</type>
</contact>
<mail>somedata</mail>
I am able to read the parent nodes i,e name, place,contact,mail. But i am unable to read child nodes i,e phone,type.
Can anyone plz help me with the code….
because you are not traversing the child nodes of course.
I would create a function and traverse it recursive.