Home > Backend Development > PHP Tutorial > Implementation code for using DOM class to read XML files in PHP_PHP Tutorial

Implementation code for using DOM class to read XML files in PHP_PHP Tutorial

WBOY
Release: 2016-07-21 15:22:31
Original
1121 people have browsed it

Main functions: Use DOM class in php to read XML files
Design knowledge points:
1. Loop reading of XML nodes
2. Use the iconv() function to implement encoding conversion to prevent Chinese garbled characters
holiday The .xml file is as follows:

Copy the code The code is as follows:




2012

New Year< /holidayName>

2012-1-1
2012-1-3


2011-12-31


;holidayName>Spring Festival

2012-1-22
2012-1-28


2012-1-21
2012-1-29
< /overTime> <🎜
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, National Day

2012-9-30
2012-10-7


2012-9-26






The php code is as follows:



Copy code

The code is as follows:

//Read 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");
//Process each year
foreach($years as $year){
//Get the specific year value
$yearNames = $year->getElementsByTagName("yearName");
$yearName = $yearNames->item( 0)->nodeValue;
echo $yearName.'year'.'
';
//Get all holidays in the year
$holidays = $year->getElementsByTagName ("holiday");
//Process each holiday
foreach($holidays as $holiday){
//Get the holiday name
$holidayNames = $holiday->getElementsByTagName( "holidayName");
$holidayName = $holidayNames->item(0)->nodeValue;
echo iconv('utf-8','gb2312', $holidayName).': '.'
';
//Get 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");
$to = $tos->item(0)->nodeValue;
echo 'Holidays are: '.$from.' to '.$to.'< ;/br>';
//Get the holiday date for the holiday
$overTimes = $holiday->getElementsByTagName("overTime");
$overTime = $overTimes->item( 0);
$days = $overTime->getElementsByTagName("day");
//By judgment, if there is a holiday date, it will be displayed, if there is not, it will not be displayed.
if($days-> length!=0){
echo 'Adjust holiday days to:';
foreach($days as $day){
echo $day->nodeValue.' ';
}
echo '
';
}
echo '
';
}
}
?>

Output display:

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324657.htmlTechArticleMain functions: Use DOM class to read XML files in php. Design knowledge points: 1. XML node loop reading 2 , use the iconv() function to implement encoding conversion to prevent the Chinese garbled holiday.xml file from being copied as follows...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template