2009-12-09 71 views
3
Warning: main() [function.main]: Node no longer exists 

我正在通過simplexml_load_file加載ml文件,它有時具有某個屬性的值,有時沒有。PHP SimpleXML錯誤信息

用法:

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

Echo $value; 

我怎麼錯誤檢查,看看是否有沒有得到警告的值。

感謝

回答

5

是否屬性()方法不返回值?它看起來可能是未設置的$ xml-> Name。嘗試,如果$ XML的檢查>名稱設置:

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

謝謝,我把isset放在了錯誤的地方..:P – Brad 2009-12-09 19:33:59

4

你必須檢查你使用任何方法從它,例如之前的節點確實存在

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

啊,你打我!我第二個答案 – ryanday 2009-12-09 19:30:13

+0

謝謝,我把isset放在了錯誤的地方..:P – Brad 2009-12-09 19:33:25

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

到目前爲止,我們只涵蓋閱讀元素的名稱和值的工作。 SimpleXML也可以訪問元素屬性。像訪問數組元素一樣訪問元素的屬性。

0

我想這樣做的:

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