Main functions: Use the DOM class in php to read XML files
Design knowledge point:
1. XML node circular reading
2. Use the iconv () function to implement encoding conversion to prevent Chinese garbled characters
The holiday. xml file is as follows:
Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<DaysOff-overTime>
<Year>
<YearName> 2012 </yearName>
<Holiday>
<HolidayName> New Year's Day <DaysOff>
<From> 2012-1-1 1-1 </from>
<To> 2012-1-3 </to>
</DaysOff>
<OverTime>
<Day> 2011-12-31 </day>
</OverTime>
</Holiday>
<Holiday>
<HolidayName> spring festival <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> Tomb Sweeping Day <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> Labor Day <DaysOff>
<From> 2012-4-29 </from>
<To> 2012-5-1 </to>
</DaysOff>
<OverTime>
<Day> 2012-4-28 </day
</OverTime>
</Holiday>
<Holiday>
<HolidayName> Dragon Boat Festival <DaysOff>
<From> 2012-6-22 </from>
<To> 2012-6-24 </to>
</DaysOff>
<OverTime/>
</Holiday>
<Holiday>
<HolidayName> Mid-Autumn Festival and National Day <DaysOff>
<From> 2012-9-30 </from>
<To> 2012-10-7 </to>
</DaysOff>
<OverTime>
<Day> 2012-9-26 </day>
</OverTime>
</Holiday>
</Year>
</DaysOff-overTime>
The php code is as follows:Copy codeThe Code is as follows: <? Php
// Read the xml file
$ XmlDoc = new DOMDocument ();
$ XmlDoc-> load ('HTTP: // 127.0.0.1/holiday. xml ');
// Obtain all the years in the xml file
$ Years = $ xmlDoc-> getElementsByTagName ("year ");
// Process each year
Foreach ($ years as $ year ){
// Obtain the specific year value
$ YearNames = $ year-> getElementsByTagName ("yearName ");
$ YearName = $ yearNames-> item (0)-> nodeValue;
Echo $ yearName. 'Year'. '</br> ';
// Obtain all holidays of the year
$ Holidays = $ year-> getElementsByTagName ("holiday ");
// Process every holiday
Foreach ($ holidays as $ holiday ){
// Obtain the holiday name
$ HolidayNames = $ holiday-> getElementsByTagName ("holidayName ");
$ HolidayName = $ holidayNames-> item (0)-> nodeValue;
Echo iconv ('utf-8', 'gb2312', $ holidayName). ':'. '</br> ';
// Obtain the specific holiday date
$ DaysOffs = $ holiday-> getElementsByTagName ("daysOff ");
$ DaysOff = $ daysOffs-> item (0 );
$ Froms = $ daysOff-> getElementsByTagName ("from ");
$ From = $ froms-> item (0)-> nodeValue;
$ Tos = $ daysOff-> getElementsByTagName ("");
$ To = $ tos-> item (0)-> nodeValue;
Echo 'holiday: '. $ from.' to '. $ to.' </br> ';
// Obtain the vacation date for this holiday
$ OverTimes = $ holiday-> getElementsByTagName ("overTime ");
$ OverTime = $ overTimes-> item (0 );
$ Days = $ overTime-> getElementsByTagName ("day ");
// After judgment, if there is a period of rest, it is displayed. If not, it is not displayed.
If ($ days-> length! = 0 ){
Echo 'Day of suspension :';
Foreach ($ days as $ day ){
Echo $ day-> nodeValue .'';
}
Echo '</br> ';
}
Echo '</br> ';
}
}
?>
Output: