开发者

Node No Longer Exists in SimpleXML

I have a problem that I cannot seem to fix. The code below will return a php error "Node no longer exists" when $array is empty. If $array is not empty it works fine. The error will show up for the line with $pr开发者_如何转开发inid = $array[0]; when $array is empty.

$doc = new SimpleXmlElement($data, LIBXML_NOCDATA); 

foreach($doc as $a => $b) {
    if ($a == 'principal-list') {
        $array = $b->principal->attributes();
    }
}

$prinid = $array[0];

if (isset($array[0])) {
    $currentuser = 1;
} else {
    $currentuser = 0;
}

Update:

Here is what I have now and I get:

Warning: count() [function.count]: Node no longer exists in * * * * * * *

$doc = new SimpleXmlElement($data, LIBXML_NOCDATA); 

foreach($doc as $a => $b) {
    if ($a == 'principal-list') {
        $array = $b->principal->attributes();
    }
}

$currentuser = 0;
if (isset($array) && count($array) > 0) {
    $prinid = $array[0];
    $currentuser = 1;
}


It means that the attribute you are trying to get isn't there. You should check that array isn't empty

if (isset($array) && count($array) > 0)
    $prinid = $array[0];


if ($a == 'principal-list' && $b && $b->principal) {
    $array = $b->principal->attributes();
}

Important thing - check $b->principal - you need check this xml object for empty. If it is true then on any try resolve $b->principal->attributes() you can get this error.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜