[英]Getting the names of child nodes in xml
Hi i am using DOM for retrieving the data from an xml file. 嗨,我正在使用DOM从xml文件中检索数据。 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.
下面的代码对于xml文件而言运行良好,但是我面临的主要问题是它只能识别根节点,而不能识别子节点。 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 这是我的xml文件
<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. 注意,我的XML包含一个任意的“ item”节点,因此值得对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:
当您单击其中一项时,它会将该项目的ID#发送到PHP脚本中……这是我处理单击的“项目”的方式:
$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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.