PHP遍歷XML文檔所有節(jié)點的方法
本文實例講述了PHP遍歷XML文檔所有節(jié)點的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
1. contact.xml代碼:
<contact id="43956"> <personal> <name> <first>J</first> <middle>J</middle> <last>J</last> </name> <title>Manager</title> <employer>National</employer> <dob>1971-12-22</dob> </personal> </contact>
2. php代碼:
<?php function walk_tree ($node, $depth = 0) { for ($i = 0, $indent = ''; $i < $depth; $i++) $indent .= ' '; if ($node->type == XML_ELEMENT_NODE) { print ($indent . $node->tagname . "\n"); $kids = $node->children (); $nkids = count ($kids); if ($nkids > 0) { $depth++; for ($i = 0; $i < $nkids; $i++) walk_tree ($kids[$i], $depth); $depth--; } } } $doc = xmldocfile ('contact.xml'); print ("<pre>\n"); walk_tree ($doc->root ()); print ("</pre>\n"); ?>
希望本文所述對大家的php程序設(shè)計有所幫助。
相關(guān)文章
php 去除html標(biāo)記--strip_tags與htmlspecialchars的區(qū)別詳解
本篇文章是對php中去除html標(biāo)記以及strip_tags與htmlspecialchars的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06PHP最常用的ini函數(shù)分析 針對PHP.ini配置文件
php的配置函數(shù)就是幾個ini_*的函數(shù),主要是針對配置文件的操作,其實就四個函數(shù):ini_get、ini_set、ini_get_all、ini_restore。個人感覺最有用的就是ini_set和ini_get。2010-04-04PHP中file_exists()判斷中文文件名無效的解決方法
這篇文章主要介紹了PHP中file_exists()判斷中文文件名無效的解決方法,是很多PHP開發(fā)人員都會遇到的問題,主要涉及對編碼的轉(zhuǎn)換,需要的朋友可以參考下2014-11-11CodeIgniter php mvc框架 中國網(wǎng)站
CodeIgniter 是一個小巧但功能強(qiáng)大的 PHP 框架,作為一個簡單而“優(yōu)雅”的工具包,它可以為 PHP 程序員建立功能完善的 Web 應(yīng)用程序。如果你是一個使用共享主機(jī),并且為客戶所要求的期限而煩惱的開發(fā)人員,如果你已經(jīng)厭倦了那些傻大笨粗的框架2008-05-05關(guān)于php程序報date()警告的處理(date_default_timezone_set)
PHP Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function2013-10-10PHP的Yii框架中YiiBase入口類的擴(kuò)展寫法示例
這篇文章主要介紹了PHP的Yii框架中YiiBase入口類的擴(kuò)展寫法示例,同時詳細(xì)講解了import和autoload這兩個YiiBase中的重要方法,需要的朋友可以參考下2016-03-03PHP內(nèi)核介紹及擴(kuò)展開發(fā)指南—基礎(chǔ)知識
本章簡要介紹一些Zend引擎的內(nèi)部機(jī)制,這些知識和Extensions密切相關(guān),同時也可以幫助我們寫出更加高效的PHP代碼。2011-09-09