PHP uses DOM to parse XML document containing Chinese characters _php tutorial

Source: Internet
Author: User
In general, we use the DOM directly in the processing of XML documents if there is Chinese in the Chinese language will be converted to garbled, the following we use the Iconv () function to achieve the encoding conversion, to prevent Chinese garbled.

The code is as follows Copy Code
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 '
';
}
}
?>

XML file

The code is as follows Copy Code



-

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




Design Knowledge Points:
1, XML node loop read
2, using Iconv () function to achieve the code conversion, to prevent Chinese garbled

http://www.bkjia.com/PHPjc/632240.html www.bkjia.com true http://www.bkjia.com/PHPjc/632240.html techarticle in general, we use the DOM directly in the processing of XML documents if there is Chinese will translate Chinese into garbled, below we use the Iconv () function to implement the code conversion, to prevent Chinese garbled ...

  • 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.