Question

I have the below XML sample file which I am trying to display using PHP:

<?xml version="1.0" encoding="UTF-8" ?>
<BroadcastData creationDate="20140326085217">
<ScheduleData>
<ChannelPeriod beginTime="20140326090000" endTime="20140402044500">
<ChannelId>Rai Uno</ChannelId>
<Event beginTime="20140326090000" duration="1800">
<EventId>260852180006</EventId>
<EventType>P</EventType>
<EpgProduction>
<EpgText language="eng">
<Name>Unomattina storie vere</Name>
</EpgText>
</EpgProduction>
</Event>
<Event beginTime="20140326093000" duration="1500">
<EventId>260852180007</EventId>
<EventType>P</EventType>
<EpgProduction>
<EpgText language="eng">
<Name>Unomattina Verde</Name>
</EpgText>
</EpgProduction>
</Event>

The below is the PHP code to parse the XML:

// Create connection
$con=mysqli_connect("localhost","test","test","epg");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$completeurl ='test.xml';
$doc = simplexml_load_file($completeurl);

foreach ($doc->ScheduleData->ChannelPeriod as $channelPeriod) {
    $channelId = $channelPeriod->ChannelId;

    foreach ($channelPeriod->Event as $event) {
        $beginTime = $event->['beginTime'];

    foreach ($channelPeriod->Event as $name) {
        $programName = $name->EpgProduction->EpgText->Name;

        printf('<p>Channel: %s<br />Begin Time: %s<br />Program Name: %s</p>', $channelId, $beginTime, $programName);

            }
        }
}

?>

For some reason the loop to show the attribute begin time is being shown repetitively for every name, the is a sample which I have when I execute the script:

Channel: Rai Uno
Begin Time: 260852180006
Program Name: Unomattina storie vere

Channel: Rai Uno
Begin Time: 260852180006
Program Name: Unomattina Verde

As you can see above the Begin time is repeated and not matching with the xml file.

Was it helpful?

Solution

Try like that:

foreach ( $doc->ScheduleData->ChannelPeriod as $channelPeriod )
{
   $channelId = $channelPeriod->ChannelId;

   foreach ( $channelPeriod->Event as $event )
   {
      $beginTime = $event['beginTime'];
      $programName = $event->EpgProduction->EpgText->Name;

      printf( '<p>Channel: %s<br />Begin Time: %s<br />Program Name: %s</p>', $channelId, $beginTime, $programName );
   }
}

It outputs:

Channel: Rai Uno
Begin Time: 20140326090000
Program Name: Unomattina storie vere

Channel: Rai Uno
Begin Time: 20140326093000
Program Name: Unomattina Verde

Also, your xml should look like this:

<?xml version="1.0" encoding="UTF-8" ?>
<BroadcastData creationDate="20140326085217">
   <ScheduleData>
      <ChannelPeriod beginTime="20140326090000" endTime="20140402044500">
         <ChannelId>Rai Uno</ChannelId>
         <Event beginTime="20140326090000" duration="1800">
            <EventId>260852180006</EventId>
            <EventType>P</EventType>
            <EpgProduction>
               <EpgText language="eng">
                  <Name>Unomattina storie vere</Name>
               </EpgText>
            </EpgProduction>
         </Event>
         <Event beginTime="20140326093000" duration="1500">
            <EventId>260852180007</EventId>
            <EventType>P</EventType>
            <EpgProduction>
               <EpgText language="eng">
                  <Name>Unomattina Verde</Name>
               </EpgText>
            </EpgProduction>
         </Event>
      </ChannelPeriod>
   </ScheduleData>
</BroadcastData>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top