Php 如何在SimpleXMLElement中访问@attributes数据?

Php 如何在SimpleXMLElement中访问@attributes数据?,php,xml,Php,Xml,我试图从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); 做 会回来的 SimpleXMLElement Object ( [recordL

我试图从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);

会回来的

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;

并获得“节点不再存在”

这将获得属性数组:

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

使用
foreach
并像
键值对一样访问它,以显示所需的值

<?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;}
}
<?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;}
}
397