0

I need to create a CRON job that are weekly going to read a XML file. The XML file contains information about all the shows at a range of cinemas. What I want to do is to read the XML file, extract the information I need about each show, and then upload each show to a database. But I run into trouble when I start nesting the for-loops. I want each tuple to contain the following information:

Tile | FilmWebNr | Rating | Version | Center | Screen | Date | Time |

The URL for the XML is http://217.144.251.113/static/Shows_FilmWeb.php

Here is a pastebin where I try to list all the dates for each screen per Title. Here is the result. As you can see, the dates is only displayed when there are more than 1 screen per Title. I dont get why the attributes array isn't always available.

I struggle with getting the last three (screen, date and time).

$map_url = "http://217.144.251.113/static/Shows_FilmWeb.php";

$response_xml_data = file_get_contents($map_url);
$data = simplexml_load_string($response_xml_data);
$array = (array) simplexml_load_string($response_xml_data);
$json  = json_encode($array);
$configData = json_decode($json, true);
$movies = $configData['Performances']['Title'];

foreach ($movies as $title) {
    echo "Title: " . $title['@attributes']['Name'] . '<br/>';
    echo "FilmWebNr: " . $title['FilmWebNum'] . '<br/>';
    echo "Rating: " . $title['TitleRating']  . '<br/>';
    echo "Version: " . $title['TitleVersion']  . '<br/>';
    echo "Center: " . $title['Center']['@attributes']['Name']  . '<br/>';
    foreach ($title['Center']['Screen'] as $screen) {
        //here I run into trouble
      }
}

Let say I try to add the following in the inner loop:

$screen['@attributes']['Name'];

I get an error saying "Undefined index: @attributes". So sometimes the attributes seems to be in an array, but sometimes not. Even though It is always a part of the XML.

3
  • 1
    And the trouble is? Commented Jan 5, 2019 at 18:32
  • Any tips on how to maybe get a proper answer? Commented Jan 5, 2019 at 20:31
  • The url to the xml is part of the code Commented Jan 6, 2019 at 12:43

1 Answer 1

1

Rather than going from XML-JSON-Arrays, it may be better to learn how to work with SimpleXML and you will find it's quite easy.

The main thing is to get used to how the various elements are layered and use foreach loops to iterate over the blocks...

$map_url = "http://217.144.251.113/static/Shows_FilmWeb.php";

$response_xml_data = file_get_contents($map_url);
$data = simplexml_load_string($response_xml_data);
$movies = $data->Performances->Title;

foreach ($movies as $title) {
    echo "Title: " . $title['Name'] . '<br/>';
    echo "FilmWebNr: " . $title->FilmWebNum . '<br/>';
    echo "Rating: " . $title->TitleRating  . '<br/>';
    echo "Version: " . $title->TitleVersion  . '<br/>';
    echo "Center: " . $title->Center['Name']  . '<br/>';
    foreach ($title->Center->Screen as $screen) {
        echo "screen:".$screen['Name']. '<br/>';
        foreach ( $screen->Date as $date )  {
            echo "Date:".$date['Name']. '<br/>';
            foreach ( $date->ShowID as $showID )  {
                echo "Time:".$showID->Time. '<br/>';
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

That makes much more sense. Thank you so much! You saved the day!

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.