Ruzzas Posted August 5, 2010 Share Posted August 5, 2010 Hello, I'm in need of help. Sometimes my PHP reads XML from a ssh and it tends to fail at times. Heres my complete error: [04-Aug-2010 18:03:16] PHP Warning: SimpleXMLElement::__construct() [<a href='simplexmlelement.--construct'>simplexmlelement.--construct</a>]: Entity: line 3: parser error : Premature end of data in tag info line 2 in /home/nmdgamin/public_html/private/includes/functions/revision.php on line 69 [04-Aug-2010 18:03:16] PHP Warning: SimpleXMLElement::__construct() [<a href='simplexmlelement.--construct'>simplexmlelement.--construct</a>]: in /home/nmdgamin/public_html/private/includes/functions/revision.php on line 69 [04-Aug-2010 18:03:16] PHP Warning: SimpleXMLElement::__construct() [<a href='simplexmlelement.--construct'>simplexmlelement.--construct</a>]: ^ in /home/nmdgamin/public_html/private/includes/functions/revision.php on line 69 [04-Aug-2010 18:03:16] PHP Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /home/nmdgamin/public_html/private/includes/functions/revision.php:69 Stack trace: #0 /home/nmdgamin/public_html/private/includes/functions/revision.php(69): SimpleXMLElement->__construct('<?xml version="...') #1 /home/nmdgamin/public_html/private/includes/functions/update.php(16): CheckRemoteRevision('SpaceBuild') #2 /home/nmdgamin/public_html/prv_updater.php(13): Update() #3 {main} thrown in /home/nmdgamin/public_html/private/includes/functions/revision.php on line 69 Remote function: function CheckRemoteRevision($Name){ if($Name){ if($Query = MySQLQuery('SELECT name, url, sv_revision FROM data WHERE name=\''. $Name .'\';')){ if($XMLFile = shell_exec('svn info \''. $Query['url'] .'\' --username anonsvn --password anonsvn --xml')){ if($DCheck = explode(' ', $XMLFile)){ if($DCheck[0] == 'svn:'){ $Revision = '0'; $Result = MySQLQuery('UPDATE data SET sv_revision=\''. $Revision .'\' WHERE name=\''. $Query['name'] .'\';'); return False; } } $XML = new SimpleXMLElement($XMLFile); foreach($XML->entry as $Attribute){ $Revision = $Attribute['revision']; } if($Query['sv_revision'] < $Revision || $Query["sv_revision"] != $Revision){ $Result = MySQLQuery('UPDATE data SET sv_revision=\''. $Revision .'\' WHERE name=\''. $Query['name'] .'\';'); } return $Revision; }else{ return False; } }else{ return False; } }else{ return False; } } How would I full proof this from getting this error again, It seems to happen at completely random times. I'm not very good with XML in PHP Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/209891-xml-simplexmlelement__construct-problem/ Share on other sites More sharing options...
JonnoTheDev Posted August 5, 2010 Share Posted August 5, 2010 Looks like badly formatted XML Quote Link to comment https://forums.phpfreaks.com/topic/209891-xml-simplexmlelement__construct-problem/#findComment-1095540 Share on other sites More sharing options...
Ruzzas Posted August 5, 2010 Author Share Posted August 5, 2010 Looks like badly formatted XML Is there a way to find if its legit XML and stop it there before erroring and re-run it so it continues with the script without any problems? Quote Link to comment https://forums.phpfreaks.com/topic/209891-xml-simplexmlelement__construct-problem/#findComment-1095553 Share on other sites More sharing options...
JonnoTheDev Posted August 5, 2010 Share Posted August 5, 2010 I would personally use the simplexml_load_string() function to return a SimpleXMLElement Object. Use the following to prevent your error. <?php libxml_use_internal_errors(true); // xml contained in $xml $xml = ""; if(!$simpleXML = simplexml_load_string($xml)) { print "Badly formatted XML document"; } else { foreach($simpleXML->entry as $Attribute){ } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/209891-xml-simplexmlelement__construct-problem/#findComment-1095562 Share on other sites More sharing options...
Ruzzas Posted August 5, 2010 Author Share Posted August 5, 2010 I have included some of your code in my script: function CheckRemoteRevision($Name){ if($Name){ if($Query = MySQLQuery('SELECT name, url, sv_revision FROM data WHERE name="'. $Name .'";')){ if($XMLFile = shell_exec('svn info "'. $Query['url'] .'" --username anonsvn --password anonsvn --xml')){ if($DCheck = explode(' ', $XMLFile)){ if($DCheck[0] == 'svn:'){ $Revision = '0'; $Result = MySQLQuery('UPDATE data SET sv_revision="'. $Revision .'" WHERE name="'. $Query['name'] .'";'); return False; } } if($XML = simplexml_load_string($XMLFile)){ $XML = new SimpleXMLElement($XMLFile); foreach($XML->entry as $Attribute){ $Revision = $Attribute['revision']; } if($Query['sv_revision'] < $Revision || $Query["sv_revision"] != $Revision){ $Result = MySQLQuery('UPDATE data SET sv_revision="'. $Revision .'" WHERE name="'. $Query['name'] .'";'); } }else{ echo "[". CurTime() ."] Addon \"". $Name ."\" seemed to have suffered an XML error. Retrying...\n"; CheckRemoteRevision($Name); } return $Revision; }else{ return False; } }else{ return False; } }else{ return False; } } I can't test it until it errors itself if it does. Thanks for the help though. Hope that fixes my stupid problem! Quote Link to comment https://forums.phpfreaks.com/topic/209891-xml-simplexmlelement__construct-problem/#findComment-1095564 Share on other sites More sharing options...
JonnoTheDev Posted August 5, 2010 Share Posted August 5, 2010 You do not need this line anymore as the function returns an object $XML = new SimpleXMLElement($XMLFile); Quote Link to comment https://forums.phpfreaks.com/topic/209891-xml-simplexmlelement__construct-problem/#findComment-1095566 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.