Jump to content

Recommended Posts

I work at a radio station in the US.  We are required to air some commercial content from our news provider.  Our commercial scheduling people are currently putting these commercials in by hand (they read the providers webpage for the schedule that tells them which commercials are to be played...and when they are to be aired).

 

I'd begun writing some code to read through their html page to download the commercials and rename them so that they can show up each day on the on-air log...and they would air when they are supposed to.

Now, the provider is providing an xml file...which should ease how I'm pulling the info...but I'm running into some problems.

 

Here's the xml:

 

<BroadcastLogs>
<DayData>
<Date>2015-05-11</Date>
<Rotation>
<Time> 6a -  7p</Time>
<SpotData>
<SpotTitle>Biore</SpotTitle>
<SpotISCI>QBIO-0065-000</SpotISCI>
<Length>30</Length>
<Type>Primary</Type>
<SpotType/><Script></Script>
</SpotData>
<SpotData>
<SpotTitle>Motel 6</SpotTitle>
<SpotISCI>YOTL-5022-000</SpotISCI>
<Length>30</Length>
<Type>Linked</Type>
<SpotType/><Script></Script>
</SpotData>
</Rotation>
<Rotation>
<Time> 6a -  7p</Time>
<SpotData>
<SpotTitle>Biore</SpotTitle>
<SpotISCI>QBIO-0065-000</SpotISCI>
<Length>30</Length>
<Type>Primary</Type>
<SpotType/><Script></Script>
</SpotData>
<SpotData>
<SpotTitle>Motel 6</SpotTitle>
<SpotISCI>YOTL-5022-000</SpotISCI>
<Length>30</Length>
<Type>Linked</Type>
<SpotType/><Script></Script>
</SpotData>
</Rotation>
<Rotation>
<Time> 6a -  7p</Time>
<SpotData>
<SpotTitle>Sprint/Boost Promotional Offer</SpotTitle>
<SpotISCI>BQRAA-4051</SpotISCI>
<Length>60</Length>
<Type>Primary</Type>
<SpotType/><Script></Script>
</SpotData>
</Rotation>
</DayData>
</BroadcastLogs>

 

You will notice that there's only one <DayData><Date>xxxx-xx-xx</Date> for the day...which shouldn't be to tough to solve. 

The part I'm having a problem with is if the commercial is 30 seconds...there are TWO ads...for every ONE <rotation> tag.  Here you will find TWO 30 seconds ads

 

 

<Rotation>
<Time> 6a -  7p</Time>
<SpotData>
<SpotTitle>Biore</SpotTitle>
<SpotISCI>QBIO-0065-000</SpotISCI>
<Length>30</Length>
<Type>Primary</Type>
<SpotType/><Script></Script>
</SpotData>
<SpotData>
<SpotTitle>Motel 6</SpotTitle>
<SpotISCI>YOTL-5022-000</SpotISCI>
<Length>30</Length>
<Type>Linked</Type>
<SpotType/><Script></Script>
</SpotData>
</Rotation>

 

If the commercial is a 60 second commercial...there's only ONE <rotation> tag...

 

 

<Rotation>
<Time> 6a -  7p</Time>
<SpotData>
<SpotTitle>Sprint/Boost Promotional Offer</SpotTitle>
<SpotISCI>BQRAA-4051</SpotISCI>
<Length>60</Length>
<Type>Primary</Type>
<SpotType/><Script></Script>
</SpotData>
</Rotation>

 

I've tried some code from the php.net site...but could seem to get the foreach's right for the difference in tags used if it's a 30 second or a 60 seconds ad.

 

Now, I'm trying this code:

 

 

<?php

function RecurseXML($xml,$parent="")
{
   $child_count = 0;
   foreach($xml as $key=>$value)
   {
      $child_count++;    
      if(RecurseXML($value,$parent.".".$key) == 0)  // no childern, aka "leaf node"
      {
       //////////the code on the following line was taken from a google search...and worked by displaying $parent, $key and $value
       //  print($parent . "." . (string)$key . " = " . (string)$value . "<br />\n");
 
 
//////  I added the next section to the code, to set variables
if ($parent . "." . (string)$key === '.DayData.Date') { $date = (string)$value; }
if ($parent . "." . (string)$key === '.DayData.Rotation.Time') { $time = (string)$value; }
if ($parent . "." . (string)$key === '.DayData.Rotation.SpotData.SpotTitle') { $title = (string)$value; }
if ($parent . "." . (string)$key === '.DayData.Rotation.SpotData.SpotISCI') { $link = (string)$value; }
if ($parent . "." . (string)$key === '.DayData.Rotation.SpotData.Length') { $length = (string)$value; }  

     }   echo "Date - " . $date . "<br />Time - " . $time . "<br />Title - " . $title . "<br />Link - " . $link . "<br />Length - " . $length . "<br />-------------------<br />";
   }
  return $child_count;  
 }

$string = file_get_contents('full url goes here');

$xml = new SimpleXMLElement($string);

RecurseXML($xml);

?>

 

The code above isn't working since the loop is off...the first line returned is the date...the next four are blank.  The next chunk has the date empty, the time is listed...and the rest in that chunk are blank...and so on.

 

What I need is the values for:

DayData->Date

DayData->Rotation->Time

DayData->Rotation->SpotData->SpotTitle

DayData->Rotation->SpotData->SpotISCI

DayData->Rotation->SpotData->Length

 

Am I even on the right path here?  I've never dealt with xml...much less parsing it!

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/296331-xml-parsing/
Share on other sites

Perhaps this will help you:

<?php

$xml = simplexml_load_file('radio.xml');
print("Day: {$xml->DayData->Date}\n");

foreach($xml->DayData->Rotation as $rotation)
{
    print("\tRotation Time: {$rotation->Time}\n");
    foreach($rotation->SpotData as $spot)
    {
        print("\t\tTitle: {$spot->SpotTitle} Length: {$spot->Length}\n");
    }
}

Running this code on the command line results in:

php -f radio.php
Day: 2015-05-11
    Rotation Time:  6a -  7p
        Title: Biore Length: 30
        Title: Motel 6 Length: 30
    Rotation Time:  6a -  7p
        Title: Biore Length: 30
        Title: Motel 6 Length: 30
    Rotation Time:  6a -  7p
        Title: Sprint/Boost Promotional Offer Length: 60
Edited by boompa
Link to comment
https://forums.phpfreaks.com/topic/296331-xml-parsing/#findComment-1511939
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.