Note that there are some explanatory texts on larger screens.

plurals
  1. POXML parser error handling
    text
    copied!<p>I would very much appreciate your help with this. Basically, I have a PHP webpage, where the user chooses a city name (by its code) and this is then sent to a script, which finds the city in a database, gets an XML file associated with it, which contains its current weather, and then displays it. It all works fine except when the user selects a code, which does not exist. What then happens is that I get the message:</p> <pre><code>Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: Entity: line 1: parser error : Start tag expected, '&lt;' not found in ... </code></pre> <p>The major problem with that is, that it stops all loading, so not only does the user see this, but also, the rest of the page including HTML and everything is just not loaded.</p> <p>What I would like is to have some sort of a check, that in case the file is not found has a wrong structure, simply echoes some message like "Error, city not found" and skips the rest of the script, but loads the rest of the webpage, its HTML etc. </p> <p>I found some solutions on the internet but I was not able to implement it successfully. </p> <p>The code that loads the actual xml looks like this:</p> <pre><code>public function __construct($query, $units = 'imperial', $lang = 'en', $appid = ''){ $xml = new SimpleXMLElement(OpenWeatherMap::getRawData($query, $units, $lang, $appid, 'xml')); $this-&gt;city = new _City($xml-&gt;city['id'], $xml-&gt;city['name'], $xml-&gt;city-&gt;coord['lon'], $xml-&gt;city-&gt;coord['lat'], $xml-&gt;city-&gt;country); etc. </code></pre> <p>In case the city is not found, instead of XML, the program gets this:</p> <p><a href="http://api.openweathermap.org/data/2.5/weather?id=123456" rel="nofollow">http://api.openweathermap.org/data/2.5/weather?id=123456</a></p> <p>In case it is found, it gets this:</p> <p><a href="http://api.openweathermap.org/data/2.5/weather?q=London&amp;mode=xml" rel="nofollow">http://api.openweathermap.org/data/2.5/weather?q=London&amp;mode=xml</a></p>
 

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