新葡亰496net 新葡亰编程 php中央银行使DOM类读取XML文件的贯彻代码_php才具_脚本之家

php中央银行使DOM类读取XML文件的贯彻代码_php才具_脚本之家

主要功能:php中使用DOM类读取XML文件 设计知识点: 1、XML节点循环读取
2、用iconv()函数实现编码转换,防止中文乱码 holiday.xml文件如下 复制代码 代码如下:

 

2012 元旦 2012-1-1 2012-1-3 2011-12-31 春节 2012-1-22 2012-1-28
2012-1-21 2012-1-29 清明节 2012-4-2 2012-4-4 2012-3-31 2012-4-1 劳动节
2012-4-29 2012-5-1 2012-4-28 端午节 2012-6-22 2012-6-24 中秋节、国庆节
2012-9-30 2012-10-7 2012-9-26 php代码如下: 复制代码 代码如下:

 代码如下

load(”); //获得该xml文件中的所有年份 $years
= $xmlDoc->getElementsByTagName; //对每一个年份进行处理 foreach{
//获得具体的年份值 $yearNames = $year->getElementsByTagName;
$yearName = $yearNames->item->nodeValue; echo $yearName.’年’.”;
//获得该年份下所有的假日 $holidays = $year->getElementsByTagName;
//对每一个假日进行处理 foreach($holidays as $holiday){ //获得假日名称
$holidayNames = $holiday->getElementsByTagName; $holidayName =
$holidayNames->item->nodeValue; echo iconv(‘utf-8′,’gb2312′,
$holidayName).’: ‘.”; //获得假日的具体放假日期 $daysOffs =
$holiday->getElementsByTagName; $daysOff = $daysOffs->item; $froms
= $daysOff->getElementsByTagName; $from =
$froms->item->nodeValue; $tos = $daysOff->getElementsByTagName;
$to = $tos->item->nodeValue; echo ‘假期为:’.$from.’ 至 ‘.$to.”;
//获得针对该假日的调休日期 $overTimes =
$holiday->getElementsByTagName; $overTime = $overTimes->item;
$days = $overTime->getElementsByTagName;
//通过判断,有调休日期则显示,没有则不显示 if{ echo ‘调休日为:’;
foreach{ echo $day->nodeValue.’ ‘; } echo ”; } echo ”; } } ?>

<?php
//读取xml文件
$xmlDoc = new DOMDocument();
$xmlDoc->load(”);
//获得该xml文件中的所有年份
$years = $xmlDoc->getElementsByTagName(“year”);
//对每一个年份进行处理
foreach($years as $year){
//获得具体的年份值
$yearNames = $year->getElementsByTagName(“yearName”);
$yearName = $yearNames->item(0)->nodeValue;
echo $yearName.’年’.'</br>’;
//获得该年份下所有的假日
$holidays = $year->getElementsByTagName(“holiday”);
//对每一个假日进行处理
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>’;
//获得针对该假日的调休日期
$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 ‘</br>’;
}
echo ‘</br>’;
}
}
?>

输出显示:

xml文件

标签:

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图