使用(at)符号读取PHP XML对象

时间:2014-05-09 10:55:46

标签: php xml file-get-contents

$nc_response = file_get_contents( $url );
$nc_return = simplexml_load_string($nc_response);

// return
SimpleXMLElement Object
(

    [DomainCheckResult] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [Domain] => mydomain.com
                    [Available] => false
                    [ErrorNo] => 0
                    [Description] => 
                )

        )

)

如何访问对象 @attributes

$nc_return->CommandResponse->DomainCheckResult->{'@attributes'} *// dont work*

1 个答案:

答案 0 :(得分:1)

使用 $object->attibuteName->attributes()->yourAttributeYouWant

直接来自文档http://www.php.net/manual/en/simplexmlelement.attributes.php

示例

<?php
$string = <<<XML
<a>
 <foo name="one" game="lonely">1</foo>
</a>
XML;

$xml = simplexml_load_string($string);
foreach($xml->foo[0]->attributes() as $a => $b) {
    echo $a,'="',$b,"\"\n";
}
?>

<强>输出

name="one"
game="lonely"