3
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

4 Answers 4

5

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();
Sign up to request clarification or add additional context in comments.

Comments

4

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();
}

1 Comment

ahh you beat me! I second this answer
0
<?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.

Comments

0

i think doing:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.