bschultz Posted May 14, 2015 Share Posted May 14, 2015 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! Quote Link to comment Share on other sites More sharing options...
boompa Posted May 14, 2015 Share Posted May 14, 2015 (edited) 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 May 14, 2015 by boompa Quote Link to comment Share on other sites More sharing options...
bschultz Posted May 14, 2015 Author Share Posted May 14, 2015 Thanks...that did the trick! So that I can understand what's going on with this code...what is the significance of the {} brackets around the XML fields names? Quote Link to comment Share on other sites More sharing options...
bschultz Posted May 14, 2015 Author Share Posted May 14, 2015 Nevermind...figured it out. I never knew you could stick variables in an echo that way. Learned TWO things today! Thanks again! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.