PHP 怎么用 simple xml 来获取 节点名称

WBOY
Release: 2016-06-13 10:23:31
Original
784 people have browsed it

PHP 如何用 simple xml 来获取 节点名称
我尝试用php simple xml的 getName() 来获取节点名称 但是不好使 不知道为什么 请大家帮看一下

xml 文件内容大致如下

 
  aaa
 


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() 不对 还是怎么?


------解决方案--------------------
没有问题呀

可能和你的php版本有关
------解决方案--------------------
XML与PHP相互转换封装函数:xmlparse.php 

PHP code
<?php $contxml = <<<XML<root>  <collection id="new1">  <category>aaa</category>  </collection>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 )?><br><font color="#e78608">------解决方案--------------------</font><br>php5.3木有问题<div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!