Jump to content

XML SimpleXMLElement::__construct() Problem


Ruzzas

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/209891-xml-simplexmlelement__construct-problem/
Share on other sites

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){

}
}
?>

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! ;)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.