Code for reading XML files using DOM class in php

WBOY
Release: 2016-07-25 08:51:53
Original
771 people have browsed it
  1. 2012
  2. 元旦
  3. 2012-1-1
  4. 2012-1-3
  5. 2011-12-31
  6. 春节
  7. 2012-1-22
  8. 2012-1-28
  9. 2012-1-21
  10. 2012-1-29
  11. 清明节
  12. 2012-4-2
  13. 2012-4-4
  14. 2012-3-31
  15. 2012-4-1
  16. 劳动节
  17. 2012-4-29
  18. 2012-5-1
  19. 2012-4-28
  20. 端午节
  21. 2012-6-22
  22. 2012-6-24
  23. 中秋节、国庆节
  24. 2012-9-30
  25. 2012-10-7
  26. 2012-9-26
复制代码

php代码如下:

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

Output display:



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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!