PHP SimpleXML error message

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

I load the ml file through simplexml_load_file, it sometimes has values ​​for the attribute, and sometimes not.

Using:

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

Echo $value; 

how can I check for an error to find out if there is a value without receiving a warning.

thanks

+3
source share
4 answers

This attribute () method does not return a value? It looks like it can't be $ xml-> Name. Try checking if $ xml-> Name is set:

if( isset($xml->Name) )
      $value = $xml->Name->attributes();
+5
source

You should check if node exists before using any method from it, e.g.

if (isset($xml->Name))
{
    $value = $xml->Name->attributes();
}
+4
source
<?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 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;
    }
}
?> 

. SimpleXML . , .

0

:

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

Source: https://habr.com/ru/post/1725198/


All Articles