2012-10-31 140 views
0
 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 

    ?> 

回答

2

使用屬性()方法:

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

試試下面的代碼:

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