Php uses dom to parse xml documents containing Chinese characters

Source: Internet
Author: User

 

The code is as follows: Copy code
<? Php
// 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'. '</br> ';
// 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). ':'. '</br> ';
// 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 '. $ to.' </br> ';
// 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 '</br> ';
}
Echo '</br> ';
}
}
?>

Xml file

The code is as follows: Copy code
<? Xml version = "1.0" encoding = "UTF-8"?>
<DaysOff-overTime>
<Year>
<YearName> 2012 </yearName>
<Holiday>
<HolidayName> New Year's Day <DaysOff>
<From> 2012-1-1 1-1 </from>
<To> 2012-1-3 </to>
</DaysOff>
<OverTime>
<Day> 2011-12-31 </day>
</OverTime>
</Holiday>
<Holiday>
<HolidayName> Spring Festival <DaysOff>
<From> 2012-1-22 </from>
<To> 2012-1-28 </to>
</DaysOff>
<OverTime>
<Day> 2012-1-21 </day>
<Day> 2012-1-29 </day>
</OverTime>
</Holiday>
<Holiday>
<HolidayName> tomb sweeping day <DaysOff>
<From> 2012-4-2 </from>
<To> 2012-4-4 </to>
</DaysOff>
<OverTime>
<Day> 2012-3-31 </day>
<Day> 2012-4-1 </day>
</OverTime>
</Holiday>
<Holiday>
<HolidayName> Labor day <DaysOff>
<From> 2012-4-29 </from>
<To> 2012-5-1 </to>
</DaysOff>
<OverTime>
<Day> 2012-4-28 </day>
</OverTime>
</Holiday>
<Holiday>
<HolidayName> Dragon Boat Festival <DaysOff>
<From> 2012-6-22 </from>
<To> 2012-6-24 </to>
</DaysOff>
<OverTime/>
</Holiday>
<Holiday>
<HolidayName> Mid-Autumn Festival and National Day <DaysOff>
<From> 2012-9-30 </from>
<To> 2012-10-7 </to>
</DaysOff>
<OverTime>
<Day> 2012-9-26 </day>
</OverTime>
</Holiday>
</Year>
</DaysOff-overTime>


Design knowledge point:
1. XML node circular reading
2. Use the iconv () function to implement encoding conversion to prevent Chinese garbled characters

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.