Use the DOM class in php to read the implementation code of the XML file. Main functions: design knowledge points for reading XML files using the DOM class in php: 1. reading XML nodes cyclically 2. Using the iconv () function for encoding conversion to prevent Chinese garbled characters. the xml file is copied as follows: 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:
The code is as follows:
2012
New Year's Day
2012-1-1
2012-1-3
2011-12-31
Spring Festival
2012-1-22
2012-1-21
2012-1-29
Tomb Sweeping Day
2012-4-2
2012-4-4
2012-3-31
2012-4-1
Labor Day
2012-4-29
2012-5-1
2012-4-28
Dragon Boat Festival
2012-6-22
2012-6-24
Mid-Autumn Festival and National Day
2012-9-30
2012-10-7
2012-9-26
The php code is as follows:
The code is as follows:
// 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 '.'
';
// 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 ).':'.'
';
// 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 '. $ .'
';
// 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'
';
}
Echo'
';
}
}
?>
Output:
Idea Design knowledge points: 1. XML node read cyclically 2. use the iconv () function to implement code conversion to prevent Chinese garbled characters. the holiday. xml file is copied as follows...