Warning: main() [function.main]: Node no longer exists
我正在通过simplexml_load_file
加载一个ml文件,有时它会有一个属性的值,有时则没有。
用法:
$value = $xml->Name->arttributes();
Echo $value;
如何在没有警告的情况下检查值是否存在错误。
谢谢
attributes()方法没有返回值吗?这似乎是$xml->Name没有设置。请检查是否设置了$xml->Name:
if( isset($xml->Name) )
$value = $xml->Name->attributes();
在使用节点的任何方法之前,您必须检查该节点是否存在,例如:
if (isset($xml->Name))
{
$value = $xml->Name->attributes();
}
<?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;
}
}
?>
我认为正在做:
$attributeValue = isset($xml->attributes()->attributeName) ? $xml->attributes()->attributeName : null;