2014-03-12 48 views
1

嘿傢伙我已經讀過關於stackoverflow buw上的同一主題的其他問題,我不知何故無法在我的場景中完成它。如何訪問SimpleXMLElement中的@attributes數據?

我想從xml文件中獲取一些特定的數據。

PHP代碼

$url = 'http://amdata.adlibsoft.com/wwwopac.ashx? 
database=AMcollect&search=priref=397*&xmltype=grouped'; 

$xml = file_get_contents($url); 
$xml = new SimpleXMLElement($xml); 

print_r ($xml); 

將返回

SimpleXMLElement Object 
(
[recordList] => SimpleXMLElement Object 
    (
     [record] => SimpleXMLElement Object 
      (
       [@attributes] => Array 
        (
         [priref] => 397 
         [created] => 2013-11-25T14:03:22 
         [modification] => 2013-11-25T18:01:23 
         [selected] => False 
        ) 

       [acquisition.date] => 1860-12-24 
       [acquisition.method] => legaat 
       [collection] => Fodor, collectie Carel Joseph 
       [credit_line] => Amsterdam Museum, legaat C.J. Fodor 
       [dimension] => Array 
        (
         [0] => SimpleXMLElement Object 
          (
           [dimension.part] => SimpleXMLElement Object 
            (
            ) 

           [dimension.type] => hoogte a 
           [dimension.unit] => cm 
           [dimension.value] => 65.5 
          ) 

         [1] => SimpleXMLElement Object 
          (
           [dimension.part] => SimpleXMLElement Object 
            (
            ) 

           [dimension.type] => breedte a 
           [dimension.unit] => cm 
           [dimension.value] => 97.5 
          ) 

         [2] => SimpleXMLElement Object 
          (
           [dimension.part] => SimpleXMLElement Object 
            (
            ) 

           [dimension.type] => hoogte b 
           [dimension.unit] => cm 
           [dimension.value] => 51.3 
          ) 

         [3] => SimpleXMLElement Object 
          (
           [dimension.part] => SimpleXMLElement Object 
            (
            ) 

           [dimension.type] => breedte b 
           [dimension.unit] => cm 
           [dimension.value] => 84.1 
          ) 

         [4] => SimpleXMLElement Object 
          (
           [dimension.part] => SimpleXMLElement Object 
            (
            ) 

           [dimension.type] => hoogte c 
           [dimension.unit] => cm 
           [dimension.value] => 40.4 
          ) 

         [5] => SimpleXMLElement Object 
          (
           [dimension.part] => SimpleXMLElement Object 
            (
            ) 

           [dimension.type] => breedte c 
           [dimension.unit] => cm 
           [dimension.value] => 80.7 
          ) 

        ) 

       [maker] => Array 
        (
         [0] => SimpleXMLElement Object 
          (
           [creator] => Kaiser, Johann Wilhelm (I) 
           [creator.date_of_birth] => 1813-01-05 
           [creator.date_of_death] => 1900-11-29 
           [creator.qualifier] => SimpleXMLElement Object 
            (
            ) 

           [creator.role] => graveur 
          ) 

         [1] => SimpleXMLElement Object 
          (
           [creator] => Helst, Bartholomeus van der 
           [creator.date_of_birth] => 1613 
           [creator.date_of_death] => 1670 
           [creator.qualifier] => naar 
          ) 

         [2] => SimpleXMLElement Object 
          (
           [creator] => Kunsthandel Frans Buffa & Zonen 
           [creator.date_of_birth] => SimpleXMLElement Object 
            (
            ) 

           [creator.date_of_death] => SimpleXMLElement Object 
            (
            ) 

           [creator.qualifier] => SimpleXMLElement Object 
            (
            ) 

           [creator.role] => uitgever 
          ) 

        ) 

       [material] => papier 
       [object_category] => prentencollectie 
       [object_name] => Array 
        (
         [0] => gravure 
         [1] => ets 
         [2] => prent 
        ) 

       [object_number] => A 11259 
       [part_of_reference] => KA 22389 & A 11217 t/m A 11265 
       [priref] => 397 
       [production.date.end] => 1860 
       [production.date.start] => 1849 
       [technique] => gegraveerd 
       [title] => De schuttersmaaltijd 
      ) 

    ) 

[diagnostic] => SimpleXMLElement Object 
    (
     [hits] => 1 
     [xmltype] => Grouped 
     [link_resolve_time] => 15.5801 
     [first_item] => 1 
     [search] => priref Equals 397* 
     [sort] => SimpleXMLElement Object 
      (
      ) 

     [limit] => 1 
     [hits_on_display] => 1 
     [response_time] => 0 
     [xml_creation_time] => 15.5801 
     [dbname] => collect 
     [dsname] => intern 
     [cgistring] => SimpleXMLElement Object 
      (
       [param] => AMcollect 
      ) 

    ) 

) 

現在讓我們說,我想我試過priref下面的事情

echo($xml->record->priref); 
echo $xml->record['priref']; 

雙方沒有透露結果(顯示沒有錯誤,沒有什麼)

然後我試圖

echo $xml->record->attributes()->priref; 

,並獲得「節點不再存在」

任何想法是什麼問題呢?

提前致謝!

回答

1

這將讓你的屬性數組:

echo $xml->recordList->record[0]->attributes(); 
0

製作使用foreach和訪問它像key-value對顯示所需的值。

<?php 
$url = 'http://amdata.adlibsoft.com/wwwopac.ashx? 
database=AMcollect&search=priref=397*&xmltype=grouped'; 

$xml = file_get_contents($url); 
$xml = simplexml_load_string($xml); 
echo "<pre>"; 
foreach ($xml->recordList->record->attributes() as $k=>$a) 
{ 
    if($k=='priref') { echo $a; break;} 
} 

OUTPUT :

397