RCB
RCB

Reputation: 99

Reading XML with PHP

I am testing out some things with reading XML using PHP. The below is a sample of the XML File:

<?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>

This is the PHP Script I built, however notthing is showing on when I run the PHP file.

<?php 

$completeurl ="test.xml";
$xml = simplexml_load_file($completeurl);

$info = $xml->BroadcastData->ScheduleData->ChannelPeriod->ChannelId;

for ($i = 0; $i++) {
$begintime = $info[$i]->Event->attributes()->beginTime;


echo "<p>Channel: ".$info."<br/>"."Begin Time: ".$begintime."</p>";
}



?>

Many thanks for your help guys !

Upvotes: 0

Views: 113

Answers (2)

Sagar Rabadiya
Sagar Rabadiya

Reputation: 4321

use following function to convert the xml string into array

  json_decode(json_encode((array)simplexml_load_string($sXML)),1);

Upvotes: 0

Ja͢ck
Ja͢ck

Reputation: 173562

You should iterate over each channel period rather than channel id (which is a sub element anyway):

$doc = simplexml_load_file($completeurl);

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

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

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

Upvotes: 1

Related Questions