Warning: main() [function.main]: Node no longer exists
我正在通过simplexml_load_file
加载ml文件,它有时会包含属性值,有时却没有。
用法:
$value = $xml->Name->arttributes();
Echo $value;
如何在没有收到警告的情况下进行错误检查以确定是否存在值。
由于
答案 0 :(得分:5)
是不是返回值的attributes()方法?它看起来可能是未设置的$ xml->名称。尝试检查是否设置了$ xml->名称:
if( isset($xml->Name) )
$value = $xml->Name->attributes();
答案 1 :(得分:4)
在使用任何方法之前,您必须检查节点是否实际存在,例如
if (isset($xml->Name))
{
$value = $xml->Name->attributes();
}
答案 2 :(得分: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Ó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;
}
}
?>
到目前为止,我们只介绍了阅读元素名称及其值的工作。 SimpleXML还可以访问元素属性。像访问数组元素一样访问元素的属性。
答案 3 :(得分:0)
我认为:
$attributeValue = isset($xml->attributes()->attributeName) ? $xml->attributes()->attributeName : null;