Implementation code for reading XML files using DOM classes in PHP _php tutorial

Source: Internet
Author: User
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 ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.