Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node No Longer Exists in SimpleXML

Tags:

php

xml

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 $prinid = $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;
}
like image 238
Adam Avatar asked Jul 29 '26 10:07

Adam


2 Answers

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];
like image 168
Ben Avatar answered Jul 31 '26 01:07

Ben


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.

like image 20
Enyby Avatar answered Jul 31 '26 03:07

Enyby



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!