0

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 !

2
  • for ($i = 0; $i > 0; $i++) { ... do you think that loop will ever start? :) Commented Mar 26, 2014 at 8:49
  • Hi @Jack, I've amnded the for loop like this, however still it is not working: for ($i = 0; $i++) { $begintime = $info[$i]->Event->attributes()->beginTime; echo "<p>Channel: ".$info."<br/>"."Begin Time: ".$begintime."</p>"; } Commented Mar 26, 2014 at 8:53

2 Answers 2

1

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);
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Many thanks for your help @jack, I have amneded the code as follows but for some reason it is still not outputting anything: <?php if (file_exists('test.xml')) { $xml = simplexml_load_file('test.xml'); } else { exit('Failed to open test.xml.'); } $doc = simplexml_load_string($xml); 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); } } ?>
I've tested my code on your partial XML and it works just fine.
Can you kindly provide me with the full code you used so I can replicate as it doesn't want to work, I am using mamp to test
@user3025379 I used simplexml_load_string(), so you just need to replace that with simplexml_load_file().
ooooooo sorry didn't notice that change, I apologies. Code is working fine. Many Thanks !. Can I use the same code to post the content into a mysql database rather then output to an HTML ?
0

use following function to convert the xml string into array

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.