如何遍历PHP中的DOM元素?
以下是XML数据(输入)-
<other data>
<foo> <bar></bar>
<value/>
<pub></pub> </foo>
<foo>
<bar></bar> <pub></pub>
</foo>
<foo>
<bar></bar> <pub></pub>
</foo>
</other data>遍历DOM对象中的元素。
示例
$elements = $dom->getElementsByTagName('foo');
$data = array();
foreach($elements as $node){
foreach($node->childNodes as $child) {
$data[] = array($child->nodeName => $child->nodeValue);
}
}输出结果
这将产生以下输出-
Every ‘foo’ tag will be iterated over and specific ‘bar’ and ‘pub’ value will be obtained, i.e specific child nodes can be accessed by their names itself.
通过在XML文件中的所有节点上运行foreach循环,可以获取XML文件中的元素。在foreach循环内,将引用主节点的子节点,并且可以访问该子节点的子值。
热门推荐
10 圣诞祝福语简短小学
11 祖国七十华诞简短祝福语
12 老师送的祝福语简短
13 生日祝福语大全女生简短
14 祝女性生日祝福语简短
15 牛年女神节祝福语简短
16 情人表白祝福语简短大气
17 老公开业祝福语简短
18 官宣新年祝福语简短