Main features: Using DOM classes to read XML files in PHP
Design Knowledge Points:
1, XML node loop read
2, using Iconv () function to achieve the code conversion, to prevent Chinese garbled
Holiday.xml files are as follows
Copy CodeThe code is as follows:
-
New
2012-1-1
2012-1-3
2011-12-31
Spring festival
2012-1-22
2012-1-28
2012-1-21
2012-1-29
Ching ming festival
2012-4-2
2012-4-4
2012-3-31
2012-4-1
Labor
2012-4-29
2012-5-1
2012-4-28
Dragon boat festival
2012-6-22
2012-6-24
Mid-Autumn Festival, National Day
2012-9-30
2012-10-7
2012-9-26
The PHP code is as follows:
Copy CodeThe code is as follows:
Reading an XML file
$xmlDoc = new DOMDocument ();
$xmlDoc->load (' http://127.0.0.1/holiday.xml ');
Get all the years in the XML file
$years = $xmlDoc->getelementsbytagname ("year");
Processing for each year
foreach ($years as $year) {
Get a specific year value
$yearNames = $year->getelementsbytagname ("Yearname");
$yearName = $yearNames->item (0)->nodevalue;
echo $yearName. ' Year '. '
';
Get all the holidays under that year
$holidays = $year->getelementsbytagname ("Holiday");
Deal with every holiday
foreach ($holidays as $holiday) {
Get a holiday name
$holidayNames = $holiday->getelementsbytagname ("Holidayname");
$holidayName = $holidayNames->item (0)->nodevalue;
echo iconv (' Utf-8 ', ' gb2312 ', $holidayName). ': '. '
';
Get a specific holiday date
$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 ' holidays: '. $from. ' to '. $to. '
';
Get the take some date for the holiday
$overTimes = $holiday->getelementsbytagname ("overtime");
$overTime = $overTimes->item (0);
$days = $overTime->getelementsbytagname ("Day");
By judging, there is take some date is displayed, no then does not show
if ($days->length!=0) {
Echo ' Take some day as: ';
foreach ($days as $day) {
echo $day->nodevalue. ' ';
}
Echo '
';
}
Echo '
';
}
}
?>
Output display:
http://www.bkjia.com/PHPjc/324657.html www.bkjia.com true http://www.bkjia.com/PHPjc/324657.html techarticle Main Features: PHP using the DOM class to read XML file design knowledge Points: 1, XML node loop read 2, with Iconv () function to implement the encoding conversion, to prevent Chinese garbled holiday.xml file as follows copy ...