2009-12-09 73 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;