0

I'm having a horrible trouble getting this XML to be properly parsed through PHP's SimpleXMLElement. The error that I'm getting (below) is looking like it's a parsing error, but I can't seem to find any issue. And, since this is the National Weather Service's alerts feed, I would have to assume others are pulling this feed and getting it to correctly work.

I've tried all the the following and several variations of them:

$simpleFeed = new SimpleXMLElement(simplexml_load_string(file_get_contents('http://alerts.weather.gov/cap/us.php?x=0')));

and

$simpleFeed = new SimpleXMLElement(simplexml_load_string('http://alerts.weather.gov/cap/us.php?x=0'));

and

$simpleFeed = new SimpleXMLElement('http://alerts.weather.gov/cap/us.php?x=0', NULL, TRUE);

I've included the error that I'm currently getting, but the line numbers do occasionally change around (I'm not sure if that's my doing or the National Weather Service's Feed's doing):

SimpleXMLElement::__construct(): Entity: line 107: parser error : Start tag expected, '<' not found
SimpleXMLElement::__construct():  
SimpleXMLElement::__construct(): ^ 

Every XML parser / validator that I run this through says that it's valid, with a few warnings. I'm not seeing anything here that would indicate the XML is the problem, except that the error message makes it look like that is the case.

Does anyone have experience with something like this and can help?

numerous
  • 52
  • 8
  • Your third code example works for me as does `$xmlObj = simplexml_load_file('http://alerts.weather.gov/cap/us.php?x=0');`. – cOle2 Aug 05 '14 at 16:43
  • Yes, it seems to be working for me as well. I'm not sure exactly what the issue was, though it could have been that the file wasn't complete at the time I loaded it, assuming there was no errors on my side (which is a stretch). – numerous Aug 06 '14 at 17:00

1 Answers1

0

To spare you some misdirections and extra-routes:

$simpleFeed = new SimpleXMLElement(simplexml_load_string(file_get_contents('http://alerts.weather.gov/cap/us.php?x=0')));

is really over-doing it. In two ways:

  1. simplexml_load_string(file_get_contents(...)) is actually just simplexml_load_file(...). PHP uses the same layer internally to get the date, regardless from within simplexml_load_file or file_get_contents. It's not hurting, but I think you should know.
  2. more hurting (in the meaning of incompatible) is that you can't instantiate a SimpleXMLElement passing the SimpleXMLElement as the first parameter of it's contructor. That just does not work. I wonder you haven't seen other errors, you might think you can "skip" those safely, but it's actually crucial you listen to error messages when developing and running software (see How to get useful error messages in PHP?).

So what does this ranting list say? Simple in code:

$feedURL = http://alerts.weather.gov/cap/us.php?x=0';
$xml     = simplexml_load_file($feedURL);
if (!$xml) {
    throw new UnexpectedValueException(
        sprintf('failed to open %s', var_export($feedURL, true))
    );
}

This little code-example will give you either a the SimpleXMLElement or will tell you otherwise. You don't have to argue whether or not the XML is valid or the parser can deal with it. For the rest of your application it would be the same. A failure to open the URL or a success. There is nothing else you need to worry about. If the URL is wrong, get the right one. If the XML might be broken, file a bug-report. Everything else is nothing you really need to worry about. Just don't put code together you have no know understanding about how it works. In that case, just read the manual how it's intended to work, double-check, trouble-shoot, listen to PHP error messages and more especially, introduce checks into your own software your own that deals with cases of failure. They are there. Design for those.

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836