PHP 如何用 simple xml 来获取 节点名称

xiaofeng0704 2012-05-21 03:20:54
我尝试用php simple xml的 getName() 来获取节点名称 但是不好使 不知道为什么 请大家帮看一下

xml 文件内容大致如下
<root>
<collection id="new1">
<category>aaa</category>
</collection>
</root>
php 代码:
$xmldoc=simplexml_load_file("test.xml");

foreach($xmldoc->children() as $child) {

foreach($child->attributes() as $a=>$b) {
echo $b;//在这打印出 collection的属性值 完全没问题 工作正常

}
echo $child->getName();//但是在这尝试打 节点名称 就出错不知道为什么

}
按道理 正常的结果应该是 new1 collection, 但是 new1 能出来, collection 怎么就打不出来, 貌似页面还出错, 我找不到原因啊。难道是getName() 不对 还是怎么?
...全文
289 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaofeng0704 2012-05-21
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

没有问题呀

可能和你的php版本有关
[/Quote]

you are right......
ndfee 2012-05-21
  • 打赏
  • 举报
回复
php5.3木有问题
  • 打赏
  • 举报
回复
XML与PHP相互转换封装函数:xmlparse.php

<?php
$contxml = <<<XML
<root>
<collection id="new1">
<category>aaa</category>
</collection>
</root>
XML;
require(dirname(__FILE__)."/xmlparse.php");
$cont = parse_xml_config($contxml,"root");
$keys = array_keys($cont);
$key1 = array_keys($cont['collection']);
print_r($cont); #Array ( [collection] => Array ( [category] => aaa ) [encoding] => UTF-8 )
print_r($keys); #Array ( [0] => collection [1] => encoding )
print_r($key1); #Array ( [0] => category )
?>
xuzuning 2012-05-21
  • 打赏
  • 举报
回复
没有问题呀

可能和你的php版本有关

21,887

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧