Code for reading XML files using the DOM class in php-php Tutorial

Source: Internet
Author: User
Code for reading XML files using the DOM class in php

  1. 2012
  2. New Year's Day
  3. 2012-1-1
  4. 2012-1-3
  5. 2011-12-31
  6. Spring Festival
  7. 2012-1-22
  8. 2012-1-21
  9. 2012-1-29
  10. Tomb Sweeping Day
  11. 2012-4-2
  12. 2012-4-4
  13. 2012-3-31
  14. 2012-4-1
  15. Labor Day
  16. 2012-4-29
  17. 2012-5-1
  18. 2012-4-28
  19. Dragon Boat Festival
  20. 2012-6-22
  21. 2012-6-24
  22. Mid-Autumn Festival and National Day
  23. 2012-9-30
  24. 2012-10-7
  25. 2012-9-26

The php code is as follows:

  1. // Read the xml file
  2. $ XmlDoc = new DOMDocument ();
  3. $ XmlDoc-> load ('http: // 127.0.0.1/holiday. XML ');
  4. // Obtain all the years in the xml file
  5. $ Years = $ xmlDoc-> getElementsByTagName ("year ");
  6. // Process each year
  7. Foreach ($ years as $ year ){
  8. // Obtain the specific year value
  9. $ YearNames = $ year-> getElementsByTagName ("yearName ");
  10. $ YearName = $ yearNames-> item (0)-> nodeValue;
  11. Echo $ yearName. 'year '.'
    ';
  12. // Obtain all holidays of the year
  13. $ Holidays = $ year-> getElementsByTagName ("holiday ");
  14. // Process every holiday
  15. Foreach ($ holidays as $ holiday ){
  16. // Obtain the holiday name
  17. $ HolidayNames = $ holiday-> getElementsByTagName ("holidayName ");
  18. $ HolidayName = $ holidayNames-> item (0)-> nodeValue;
  19. Echo iconv ('utf-8', 'gb2312', $ holidayName ).':'.'
    ';
  20. // Obtain the specific holiday date
  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 ("");
  26. $ To = $ tos-> item (0)-> nodeValue;
  27. Echo 'holiday: '. $ from.' to '. $ .'
    ';
  28. // Obtain the vacation date for this holiday
  29. $ OverTimes = $ holiday-> getElementsByTagName ("overTime ");
  30. $ OverTime = $ overTimes-> item (0 );
  31. $ Days = $ overTime-> getElementsByTagName ("day ");
  32. // After judgment, if there is a period of rest, it is displayed. if not, it is not displayed.
  33. If ($ days-> length! = 0 ){
  34. Echo 'Day of suspension :';
  35. Foreach ($ days as $ day ){
  36. Echo $ day-> nodeValue .'';
  37. }
  38. Echo'
    ';
  39. }
  40. Echo'
    ';
  41. }
  42. }
  43. ?>

Output:

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.