php使用dom解析含有中文字符xml文档

一般我们直接使用dom来处理xml文档时如果里面有中文就会把中文转换成乱码了,下面我们通过使用iconv()函数实现编码转换,防止中文乱码.代码如下:

  1. <?php
  2. //读取xml文件
  3. $xmlDoc = new DOMDocument();
  4. $xmlDoc->load('http://127.0.0.1/holiday.xml');
  5. //获得该xml文件中的所有年份
  6. $years = $xmlDoc->getElementsByTagName("year");
  7. //对每一个年份进行处理
  8. foreach($years as $year){
  9. //获得具体的年份值
  10. $yearNames = $year->getElementsByTagName("yearName");
  11. $yearName = $yearNames->item(0)->nodeValue;
  12. echo $yearName.'年'.'</br>';
  13. //获得该年份下所有的假日
  14. $holidays = $year->getElementsByTagName("holiday");
  15. //对每一个假日进行处理
  16. foreach($holidays as $holiday){
  17. //获得假日名称
  18. $holidayNames = $holiday->getElementsByTagName("holidayName");
  19. $holidayName = $holidayNames->item(0)->nodeValue;
  20. echo iconv('utf-8','gb2312', $holidayName).': '.'</br>';
  21. //获得假日的具体放假日期
  22. $daysOffs = $holiday->getElementsByTagName("daysOff");
  23. $daysOff = $daysOffs->item(0);
  24. $froms = $daysOff->getElementsByTagName("from");
  25. $from = $froms->item(0)->nodeValue;
  26. $tos = $daysOff->getElementsByTagName("to");
  27. $to = $tos->item(0)->nodeValue;
  28. echo '假期为:'.$from.' 至 '.$to.'</br>';
  29. //获得针对该假日的调休日期
  30. $overTimes = $holiday->getElementsByTagName("overTime");
  31. $overTime = $overTimes->item(0);
  32. $days = $overTime->getElementsByTagName("day");
  33. //通过判断,有调休日期则显示,没有则不显示
  34. if($days->length!=0){
  35. echo '调休日为:';
  36. foreach($days as $day){
  37. echo $day->nodeValue.' ';
  38. }
  39. echo '</br>';
  40. }
  41. echo '</br>';
  42. }
  43. }
  44. ?>

xml文件代码如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <daysOff-overTime>
  3. <year>
  4. <yearName>2012</yearName>
  5. <holiday>
  6. <holidayName>元旦</holidayName>
  7. <daysOff>
  8. <from>2012-1-1</from>
  9. <to>2012-1-3</to>
  10. </daysOff>
  11. <overTime>
  12. <day>2011-12-31</day>
  13. </overTime>
  14. </holiday>
  15. <holiday>
  16. <holidayName>春节</holidayName>
  17. <daysOff>
  18. <from>2012-1-22</from>
  19. <to>2012-1-28</to>
  20. </daysOff>
  21. <overTime>
  22. <day>2012-1-21</day>
  23. <day>2012-1-29</day>
  24. </overTime>
  25. </holiday>
  26. <holiday>
  27. <holidayName>清明节</holidayName>
  28. <daysOff>
  29. <from>2012-4-2</from>
  30. <to>2012-4-4</to>
  31. </daysOff>
  32. <overTime>
  33. <day>2012-3-31</day>
  34. <day>2012-4-1</day>
  35. </overTime>
  36. </holiday>
  37. <holiday>
  38. <holidayName>劳动节</holidayName>
  39. <daysOff>
  40. <from>2012-4-29</from>
  41. <to>2012-5-1</to>
  42. </daysOff>
  43. <overTime>
  44. <day>2012-4-28</day>
  45. </overTime>
  46. </holiday>
  47. <holiday>
  48. <holidayName>端午节</holidayName>
  49. <daysOff>
  50. <from>2012-6-22</from>
  51. <to>2012-6-24</to>
  52. </daysOff>
  53. <overTime/>
  54. </holiday>
  55. <holiday>
  56. <holidayName>中秋节、国庆节</holidayName>
  57. <daysOff>
  58. <from>2012-9-30</from>
  59. <to>2012-10-7</to>
  60. </daysOff>
  61. <overTime>
  62. <day>2012-9-26</day>
  63. </overTime>
  64. </holiday>
  65. </year>
  66. </daysOff-overTime>
设计知识点:

1、XML节点循环读取

2、用iconv()函数实现编码转换,防止中文乱码