【问题标题】:How to get the value element [@attributes] [duplicate]如何获取值元素 [@attributes] [重复]
【发布时间】:2012-10-20 23:09:28
【问题描述】:
     SimpleXMLElement Object
 (
        [@attributes] => Array
            (
                [projectTypeID] => 10
                [genContentID] => 000001
                [language] => en
                [title] => Travel to developing countries
                [subContent] => default
            )

        [versionInfo] => SimpleXMLElement Object
            (
                [@attributes] => Array
                    (
                        [reviewDate] => 2/20/2012
                        [reviewedBy] => Harvey Simon
                    )

            )
     ) 

如何获取@attributes 元素的值?

   [title] => Travel to developing countries

   [reviewDate] => 2/20/2012

这是我的代码

  <?php   $xml = simplexml_load_file('000001.xml','SimpleXMLElement', LIBXML_NOCDATA);


          echo "<pre>";
           print_r($xml);
          echo "</pre>";

             echo $xml->{'@attributes'}['title'];  // not working
        echo "<br/>";
       echo $xml->versionInfo->{'@attributes'}['reviewDate'];   // not working

   ?>

【问题讨论】:

    标签: php xml


    【解决方案1】:

    试试下面的代码:

    foreach($xml as $key => $value){
          foreach($value->attributes() as $attributeskey0 => $attributesvalue1){
                  echo "________[$attributeskey0] = $attributesvalue1";
          }
    }
    

    【讨论】:

      【解决方案2】:

      使用 attributes() 方法:

      $attrs = $xml->versionInfo->attributes();
      echo $attrs['reviewDate'];
      

      【讨论】:

        猜你喜欢
        • 2018-07-04
        • 1970-01-01
        • 2011-09-03
        • 2014-11-22
        • 1970-01-01
        • 1970-01-01
        • 2019-12-09
        • 2021-08-04
        • 1970-01-01
        相关资源
        最近更新 更多