|
設(shè)計(jì)知識(shí)點(diǎn):
1、XML節(jié)點(diǎn)循環(huán)讀取
2、用iconv()函數(shù)實(shí)現(xiàn)編碼轉(zhuǎn)換,防止中文亂碼
holiday.xml文件如下
復(fù)制代碼 代碼如下:
<?xml version="1.0" encoding="UTF-8"?>
<daysOff-overTime>
<year>
<yearName>2012</yearName>
<holiday>
<holidayName>元旦</holidayName>
<daysOff>
<from>2012-1-1</from>
<to>2012-1-3</to>
</daysOff>
<overTime>
<day>2011-12-31</day>
</overTime>
</holiday>
<holiday>
<holidayName>春節(jié)</holidayName>
<daysOff>
<from>2012-1-22</from>
<to>2012-1-28</to>
</daysOff>
<overTime>
<day>2012-1-21</day>
<day>2012-1-29</day>
</overTime>
</holiday>
<holiday>
<holidayName>清明節(jié)</holidayName>
<daysOff>
<from>2012-4-2</from>
<to>2012-4-4</to>
</daysOff>
<overTime>
<day>2012-3-31</day>
<day>2012-4-1</day>
</overTime>
</holiday>
<holiday>
<holidayName>勞動(dòng)節(jié)</holidayName>
<daysOff>
<from>2012-4-29</from>
<to>2012-5-1</to>
</daysOff>
<overTime>
<day>2012-4-28</day>
</overTime>
</holiday>
<holiday>
<holidayName>端午節(jié)</holidayName>
<daysOff>
<from>2012-6-22</from>
<to>2012-6-24</to>
</daysOff>
<overTime/>
</holiday>
<holiday>
<holidayName>中秋節(jié)、國慶節(jié)</holidayName>
<daysOff>
<from>2012-9-30</from>
<to>2012-10-7</to>
</daysOff>
<overTime>
<day>2012-9-26</day>
</overTime>
</holiday>
</year>
</daysOff-overTime>
php代碼如下:
復(fù)制代碼 代碼如下:
<?php
//讀取xml文件
$xmlDoc = new DOMDocument();
$xmlDoc->load('http://127.0.0.1/holiday.xml');
//獲得該xml文件中的所有年份
$years = $xmlDoc->getElementsByTagName("year");
//對(duì)每一個(gè)年份進(jìn)行處理
foreach($years as $year){
//獲得具體的年份值
$yearNames = $year->getElementsByTagName("yearName");
$yearName = $yearNames->item(0)->nodeValue;
echo $yearName.'年'.'</br>';
//獲得該年份下所有的假日
$holidays = $year->getElementsByTagName("holiday");
//對(duì)每一個(gè)假日進(jìn)行處理
foreach($holidays as $holiday){
//獲得假日名稱
$holidayNames = $holiday->getElementsByTagName("holidayName");
$holidayName = $holidayNames->item(0)->nodeValue;
echo iconv('utf-8','gb2312', $holidayName).': '.'</br>';
//獲得假日的具體放假日期
$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.'</br>';
//獲得針對(duì)該假日的調(diào)休日期
$overTimes = $holiday->getElementsByTagName("overTime");
$overTime = $overTimes->item(0);
$days = $overTime->getElementsByTagName("day");
//通過判斷,有調(diào)休日期則顯示,沒有則不顯示
if($days->length!=0){
echo '調(diào)休日為:';
foreach($days as $day){
echo $day->nodeValue.' ';
}
echo '</br>';
}
echo '</br>';
}
}
?>
輸出顯示:
php技術(shù):php中使用DOM類讀取XML文件的實(shí)現(xiàn)代碼,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時(shí)間聯(lián)系我們修改或刪除,多謝。