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

时间:2014-03-12 07:41:35

标签: php xml

我正在尝试从xml文件中获取一些特定数据。

php Code

$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;

并得到"节点不再存在"

2 个答案:

答案 0 :(得分:1)

这将获得属性数组:

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

答案 1 :(得分: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