简体   繁体   中英

Getting the names of child nodes in xml

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.

 function addNodesToValue($value,$nodelist) {
   for ($i = 0; $i < $nodelist->length; $i++) {
     $childnode['name']=$nodelist->item($i)->nodeName;
     $childnode['value']=$nodelist->item($i)->nodeValue;
     $value[ $childnode['name']] = $childnode['value'];
     // recursive traverse child nodes
     if($nodelist->item($i)->hasChildNodes()){
       $value = addNodesToValue($value,$nodelist->item($i)->childNodes);
     }
    }
    return $value;
 } 

//usage
$value = addNodesToValue($value,$link);

What about this?

function get_names($node) {
    $names = array();
    foreach ($node->childNodes as $child) {
        if ($child->nodeType == 1) {
            $names[] = $child->nodeName;
            if ($child->hasChildNodes()) {
                $names[] = get_names($child);
            }
        }
    }
    return array_filter($names);
}

$xml = '
<root>
    <name>John</name>
    <place>Australia</place>
    <contact> 
        <phone>8734563485</phone> 
        <type>Mobile</type> 
    </contact>
    <mail>somedata</mail>
</root>';
$names = get_names( DOMDocument::loadXML($xml)->firstChild );
print_r($names);

Output;

Array
(
    [0] => name
    [2] => place
    [4] => contact
    [5] => Array
        (
            [0] => phone
            [2] => type
        )

    [6] => mail
)

SOLVED:

Note, my XML contains an arbitrary 'item' node, so worth testing with your XML. Here's mine:

<news>
 <item>
  <date>9/2/2014</date>
  <title>Home Depot Breach</title>
  <content>NEW YORK (AP) - My store may be the latest retailer</content>
 </item>
 <item>
  <date>8/28/2014</date>
  <title>Dairy Queen</title>
  <content>Dairy Queen has the best ice cream</content>
 </item>
</news>

I have a page that lists the items in a table. When you click on one of the items, it sends the ID# of that item into a PHP script ... here's how I process the 'item' clicked:

$xml = DOMDocument::load('news.xml');
$id  = $_GET['i'];

$date    = $xml->getElementsByTagName('date')->item($id)->nodeValue;
$title   = $xml->getElementsByTagName('title')->item($id)->nodeValue;
$content = $xml->getElementsByTagName('content')->item($id)->nodeValue;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM