Jump to content

Recommended Posts

My script:

 

$feed = "http://www.mysite.com/feed.xml";

$doc = new DOMDocument();
$doc->load($feed);

$entries = $doc->getElementsByTagName("entry");

$datesUsed = array();

foreach ($entries as $entry) {
        $status = $entry->getElementsByTagName("eventStatus");
        $eventStatus = $status->item(0)->getAttributeNode("value")->value;

        if ($eventStatus == $confirmed) {
                $titles = $entry->getElementsByTagName("title");
                $title = $titles->item(0)->nodeValue;

                $times = $entry->getElementsByTagName("when");

                $startTime = $times->item(0)->getAttributeNode("startTime")->value;
                $date = date(" F j, Y", strtotime($startTime));
                $time = date("g:i A", strtotime($startTime));

                if (!in_array($date, $datesUsed)) {
                        $currentnum = count($datesUsed);
                        if ($currentnum > 0) echo "<br />";
                        echo "<strong>$date</strong><br />";
                        $datesUsed[$currentnum+1] = $date;
                        $arrayyes = 1;
                }

                echo $title . " - $time<br />";
        }
}

 

The script pulls out the “when” element and its attributes. The problem is that not every entry has a when element. How can I skip these records? I am aware of a hasAttribute function, but I can’t seem to find any equivalent for elements. I’ve played around with several techniques, and nothing has worked.

 

If these entries are not skipped, the following error occurs:

Fatal error: Call to a member function getAttributeNode() on a non-object

 

Thanks in advance.

<?php
$feed = "http://www.mysite.com/feed.xml";

$doc = new DOMDocument();
$doc->load($feed);

$entries = $doc->getElementsByTagName("entry");

$datesUsed = array();

foreach ($entries as $entry) {
        $status = $entry->getElementsByTagName("eventStatus");
        $eventStatus = $status->item(0)->getAttributeNode("value")->value;

        if ($eventStatus == $confirmed) {
                $titles = $entry->getElementsByTagName("title");
                $title = $titles->item(0)->nodeValue;

                $times = $entry->getElementsByTagName("when");

                if (!is_object($times))
                      continue; // skip the rest of the loop if times is not an object

                $startTime = $times->item(0)->getAttributeNode("startTime")->value;
                $date = date(" F j, Y", strtotime($startTime));
                $time = date("g:i A", strtotime($startTime));

                if (!in_array($date, $datesUsed)) {
                        $currentnum = count($datesUsed);
                        if ($currentnum > 0) echo "<br />";
                        echo "<strong>$date</strong><br />";
                        $datesUsed[$currentnum+1] = $date;
                        $arrayyes = 1;
                }

                echo $title . " - $time<br />";
        }
}
?>

 

is_object test if $times is an object, if it is not skip the rest and continue the loop.

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.