Solved

PHP Parsing multiple XML files

Posted on 2011-03-20
1
375 Views
Last Modified: 2012-05-11
Hello,

I am trying to make a page which parses a series of XML files in order to build a schedule page. The purpose is to make it easier for the client to change schedule information by simply uploading xml files. So far, I can get the page to parse the first XML file. It also recognizes the EXISTENCE of the other two test xml files, but it does not seem to be parsing them. here is the code that I am using:

<div id="copycol">
        <h1>Calendar of Events</h1>
        <p>&nbsp;</p>
        <div class="indent">
              <p>Click an event to open that event's registration page</p>
        </div>
        <p>&nbsp;</p>
<?php
$_MONTH = array("JANUARY","FEBRUARY","MARCH","APRIL","MAY","JUNE","JULY","AUGUST","SEPTEMBER","OCTOBER","NOVEMBER","DECEMBER");
//$_CURRENTYEAR = gmdate("Y");
$_CURRENTDATE = date("m/d/Y");
$_CURRENTMONTH = gmdate("n") - 1;

/*
echo "<p>".$_CURRENTYEAR."</p>";
echo "<p>".$_CURRENTDATE."</p>";
echo "<p>".$_CURRENTMONTH."</p>";
echo "<p>".$MONTH[$_CURRENTMONTH]."</p>";
*/

      function start_tag($parser, $name, $attribs) {
            if ($name == "MONTH"){
                  echo "<div class=\"month\">";
            }
            if ($name == "MONTH_NAME"){
                  echo "<div class=\"month_name\">";
            }
            if ($name == "DATE"){
                  echo "<div class=\"date\">";
            }
            if ($name == "REGISTRATION"){
                  echo "<a href=\"registration.html\">";
            }      
            if ($name == "EVENT"){
                  echo "<div class=\"event\">";
            }
      }
      function end_tag($parser, $name) {      
            if ($name == "REGISTRATION"){
                  echo "</a>";
            }      
            else {
            echo "</div>";
            }
      }
      function tag_contents($parser, $data) {
            echo $data;
      }
      //Initialize the XML parser
      if (! ($xml_parser = xml_parser_create()) ){
            die ("Cannot create parser");
      }
      xml_set_element_handler($xml_parser, "start_tag", "end_tag");
      xml_set_character_data_handler($xml_parser, "tag_contents");
for ($i = 0;$i < 12; $i++){
      $_CURRENTYEAR = gmdate("Y");
      $_OFFSET = $_CURRENTMONTH + $i;
      if ($_OFFSET > 11){
            $_OFFSET = $_OFFSET - 12;
            $_CURRENTYEAR++;
      }
      $_CALENDARMONTH = $_MONTH[$_OFFSET];
      $_CALENDARFILE = $_CALENDARMONTH."_".$_CURRENTYEAR.".xml";
      echo "<p>".$_CALENDARFILE."</p>\n";
      if (file_exists($_CALENDARFILE)){
            echo "<p>".$_OFFSET." Offset</p>\n";
            if (!($handle = fopen($_CALENDARFILE, "r"))){
                  die("cannot open ".$_CALENDARFILE);
            }
            while ($data = fread($handle, 8000)){
                  xml_parse($xml_parser, $data, feof($handle));
            }
            fclose($handle);
      }
      xml_parser_free($xml_parser);      
}
?>
</div>

The current code outputs the first month's schedule, but then only outputs the names of the other two files and their associated $_OFFSET values. But it does not output the schedule from the XML files.

Can someone PLEASE help me with this. I have spent several hours researching this and have found nothing of help.

Thank you in advance,

~Randal
0
Comment
Question by:Brunke76
1 Comment
 
LVL 17

Accepted Solution

by:
Chris Harte earned 500 total points
ID: 35181887
Does this line

while ($data = fread($handle, 8000)){

include the EOF of the read file, because feof only returns true on EOF.

Other than that use

$data = file_get_contents ($_CALENDARFILE);
xml_parse ($xml_parser, $data,  true);

to read and parse the file
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question