Note that there are some explanatory texts on larger screens.

plurals
  1. POisset vs try/catch on namespaces
    text
    copied!<p>I'm parsing RSS feeds and have a (sub)function that looks like this:</p> <pre><code>function nSpace($nSp, $type, $i){ $namespaces = $i-&gt;getNameSpaces(true); $exp = explode(':', $nSp); try{ $nameSp = $i-&gt;children($namespaces[$exp[0]]); $item[$type] = (string)$nameSp-&gt;$exp[1]; return $item[$type]; } catch (Exception $e) { } } </code></pre> <p>I'm trying to retrieve the namespace value, and will pass common RSS feed namespaces like "dc:date" or "content:encoded" for example as $nSp. The function works fine should the namespace exist in the XML, however the try{} is producing an 'Undefined Index' error in the first line when it doesn't.</p> <p>Personally I'd rather run an <code>isset($i-&gt;children($namespaces[$exp[0]])){}</code> check instead of the try/catch, as I'm more familiar with that workflow, however this doesn't work (get a 'Can't use return value' error).</p> <p>Few questions:</p> <ol> <li>Shouldn't the try{} not produce error messages? </li> <li>Is try/catch the best way? </li> <li>Is there a way to do it with an if() instead? </li> </ol> <p>Thanks.</p> <p>Update: Here is the (abreviated) call/usage for this function:</p> <pre><code>$rawFeed = file_get_contents($url); try { $rss = new SimpleXMLElement($rawFeed); } catch (Exception $e) { } foreach ($rss-&gt;channel-&gt;item as $i) { $item['link'] = isset($i-&gt;link) ? (string)$i-&gt;link : nSpace('dc:link', 'link', $i); $item['dateRaw'] = isset($i-&gt;pubDate) ? (string)$i-&gt;pubDate : nSpace('dc:date', 'date', $i); // etc... } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload