【问题标题】:Read PHP XML object with (at) sign读取带有 (at) 符号的 PHP XML 对象
【发布时间】:2014-05-09 10:55:46
【问题描述】:
$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*

【问题讨论】:

    标签: php xml file-get-contents


    【解决方案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"
    

    【讨论】:

      猜你喜欢
      • 2013-06-20
      • 1970-01-01
      • 2013-04-24
      • 1970-01-01
      • 2011-08-13
      • 1970-01-01
      • 2017-03-08
      • 2011-10-19
      • 1970-01-01
      相关资源
      最近更新 更多