DOMDocument解析一个多层次的xml结构的文档

Jiari.xml文档:

<?xmlversion="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>春节</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>清明节</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>劳动节</holidayName>

            <daysOff>

               <from>2012-4-29</from>

                <to>2012-5-1</to>

            </daysOff>

            <overTime>

               <day>2012-4-28</day>

            </overTime>

        </holiday>

 

        <holiday>

            <holidayName>端午节</holidayName>

            <daysOff>

               <from>2012-6-22</from>

                <to>2012-6-24</to>

            </daysOff>

        <overTime/>

    </holiday>

 

    <holiday>

        <holidayName>中秋节、国庆节</holidayName>

        <daysOff>

            <from>2012-9-30</from>

            <to>2012-10-7</to>

        </daysOff>

        <overTime>

            <day>2012-9-26</day>

        </overTime>

    </holiday>

    </year>

</daysOff-overTime>

 

DOMDocument对象遍历输出假日数据:

<?php

//读取xml文件

$xmlDoc= new DOMDocument();

$xmlDoc->load('http://127.0.0.1/holiday.xml');

 

//获得该xml文件中的所有年份

$years =$xmlDoc->getElementsByTagName("year");

 

//对每一个年份进行处理

foreach($yearsas $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>';

    }

}

?>

 

制作人:飞虎                                           无兄弟不编程!

=====================================================================

欢迎加QQ群进行更多交流:305397511     专注于phpmysqljquery以及开源框架

posted @ 2014-06-06 17:27  飞虎cnblog  阅读(385)  评论(0编辑  收藏  举报
友情链接:技术迷 | JSM官方博客 | 阿旭博客 | 有声小说在线听