Reputation: 2237
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
Upvotes: 3
Views: 3184
Reputation: 60413
i think doing:
$attributeValue = isset($xml->attributes()->attributeName) ? $xml->attributes()->attributeName : null;
Upvotes: 0
Reputation: 5801
<?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Ó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.
Upvotes: 0
Reputation: 2516
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();
Upvotes: 5
Reputation: 28730
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();
}
Upvotes: 4