Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP SimpleXML错误消息_Php_Simplexml - Fatal编程技术网

PHP SimpleXML错误消息

PHP SimpleXML错误消息,php,simplexml,Php,Simplexml,我通过simplexml\u load\u file加载一个ml文件,它有时有属性值,有时没有 用法: Warning: main() [function.main]: Node no longer exists 如何在没有得到警告的情况下检查是否存在值 谢谢在使用该节点的任何方法之前,您必须检查该节点是否确实存在,例如: $value = $xml->Name->arttributes(); Echo $value; 是attributes()方法没有返回值吗?看起来可能

我通过
simplexml\u load\u file
加载一个ml文件,它有时有属性值,有时没有

用法:

Warning:  main() [function.main]: Node no longer exists
如何在没有得到警告的情况下检查是否存在值


谢谢

在使用该节点的任何方法之前,您必须检查该节点是否确实存在,例如:

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

Echo $value; 
是attributes()方法没有返回值吗?看起来可能是未设置的$xml->Name。尝试检查是否设置了$xml->Name:

if (isset($xml->Name))
{
    $value = $xml->Name->attributes();
}

PHP:解析器背后
科德女士
艾克托拉酒店
科德先生
El法案Ó;R
所以,这种语言。就像一种编程语言。或者它是一个
脚本语言?这一惊心动魄的恐怖恶搞揭示了一切
一部纪录片。
PHP解决了我所有的网络问题
7.
5.
XML;
?>
到目前为止,我们只讨论了读取元素名称及其值的工作。SimpleXML还可以访问元素属性。访问元素的属性,就像访问数组的元素一样

我认为:

<?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;
    }
}
?> 

啊,你打败我了!我赞成这个答案
<?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;
    }
}
?> 
$attributeValue = isset($xml->attributes()->attributeName) ? $xml->attributes()->attributeName : null;