PHP 怎麼用 simple xml 來擷取 節點名稱

來源:互聯網
上載者:User
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
    aaa  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 )?>
------解決方案--------------------
php5.3木有問題
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.