views:

269

answers:

4

Warning: main() [function.main]: Node no longer exists

I am loading a ml file via simplexml_load_file, it sometimes has values for an attribute and sometimes does not.

Usage:

$value = $xml->Name->arttributes();

Echo $value;

how do I error check to see if there is a value without getting a warning.

Thanks

+3  A: 

You have to check that the node actually exists before you use any method from it, e.g.

if (isset($xml->Name))
{
    $value = $xml->Name->attributes();
}
Josh Davis
ahh you beat me! I second this answer
ryanday
Thanks, I was putting the isset in the wrong place.. :P
Brad
+1  A: 

Is it the attributes() method not returning a value? It looks like it may be the $xml->Name that isn't set. Try checking if $xml->Name is set:

if( isset($xml->Name) )
      $value = $xml->Name->attributes();
ryanday
Thanks, I was putting the isset in the wrong place.. :P
Brad
A: 
<?php
$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<movies>
 <movie>
  <title>PHP: Behind the Parser</title>
  <characters>
   <character>
    <name>Ms. Coder</name>
    <actor>Onlivia Actora</actor>
   </character>
   <character>
    <name>Mr. Coder</name>
    <actor>El Act&#211;r</actor>
   </character>
  </characters>
  <plot>
   So, this language. It's like, a programming language. Or is it a
   scripting language? All is revealed in this thrilling horror spoof
   of a documentary.
  </plot>
  <great-lines>
   <line>PHP solves all my web problems</line>
  </great-lines>
  <rating type="thumbs">7</rating>
  <rating type="stars">5</rating>
 </movie>
</movies>
XML;
?>

<?php
include 'example.php';

$xml = new SimpleXMLElement($xmlstr);

/* Access the <rating> nodes of the first movie.
 * Output the rating scale, too. */
foreach ($xml->movie[0]->rating as $rating) {
    switch((string) $rating['type']) { // Get attributes as element indices
    case 'thumbs':
        echo $rating, ' thumbs up';
        break;
    case 'stars':
        echo $rating, ' stars';
        break;
    }
}
?>

So far, we have only covered the work of reading element names and their values. SimpleXML can also access element attributes. Access attributes of an element just as you would elements of an array.

moustafa
A: 

i think doing:

$attributeValue = isset($xml->attributes()->attributeName) ? $xml->attributes()->attributeName : null;
prodigitalson