时间:2021-07-01 10:21:17 帮助过:2人阅读
代码如下 | |
//读取xml文件 $xmlDoc = new DOMDocument(); $xmlDoc->load('http://127.0.0.1/holiday.xml'); //获得该xml文件中的所有年份 $years = $xmlDoc->getElementsByTagName("year"); //对每一个年份进行处理 foreach($years as $year){ //获得具体的年份值 $yearNames = $year->getElementsByTagName("yearName"); $yearName = $yearNames->item(0)->nodeValue; echo $yearName.'年'.''; //获得该年份下所有的假日 $holidays = $year->getElementsByTagName("holiday"); //对每一个假日进行处理 foreach($holidays as $holiday){ //获得假日名称 $holidayNames = $holiday->getElementsByTagName("holidayName"); $holidayName = $holidayNames->item(0)->nodeValue; echo iconv('utf-8','gb2312', $holidayName).': '.''; //获得假日的具体放假日期 $daysOffs = $holiday->getElementsByTagName("daysOff"); $daysOff = $daysOffs->item(0); $froms = $daysOff->getElementsByTagName("from"); $from = $froms->item(0)->nodeValue; $tos = $daysOff->getElementsByTagName("to"); $to = $tos->item(0)->nodeValue; echo '假期为:'.$from.' 至 '.$to.''; //获得针对该假日的调休日期 $overTimes = $holiday->getElementsByTagName("overTime"); $overTime = $overTimes->item(0); $days = $overTime->getElementsByTagName("day"); //通过判断,有调休日期则显示,没有则不显示 if($days->length!=0){ echo '调休日为:'; foreach($days as $day){ echo $day->nodeValue.' '; } echo ''; } echo ''; } } ?> |
xml文件
代码如下 | |
|
设计知识点:
1、XML节点循环读取
2、用iconv()函数实现编码转换,防止中文乱码
http://www.bkjia.com/PHPjc/632240.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/632240.htmlTechArticle一般我们直接使用dom来处理xml文档时如果里面有中文就会把中文转换成乱码了,下面我们通过使用iconv()函数实现编码转换,防止中文乱码...