访问SimpleXML属性

时间:2014-10-09 22:08:36

标签: php xml simplexml

我刚开始使用Simple XML

将此数据解析为$ scorexml

SimpleXMLElement Object
(
[@attributes] => Array
    (
        [query-date-time] => 20141009T175358-0400
        [query-string] => hidden
        [hostname] => hidden.com
        [result-count] => 1
        [error-count] => 0
        [total-count] => 9
        [elapsed-time] => 13.0ms
    )

[sports-content] => SimpleXMLElement Object
    (
        [sports-metadata] => SimpleXMLElement Object
            (
                [@attributes] => Array
                    (
                        [doc-id] => xt.21794675-Golf_Leaderboard_XML
                        [date-time] => 20140914T150400-0400
                        [language] => en-US
                        [document-class] => event-summary
                        [fixture-key] => leaderboard
                        [revision-id] => xt.21792812-Golf_Leaderboard_XML
                    )

                [sports-title] => $8,000,000 The Tour Championship Leaderboard
                [sports-content-codes] => SimpleXMLElement Object
                    (
                        [sports-content-code] => Array
                            (
                                [0] => SimpleXMLElement Object
                                    (
                                        [@attributes] => Array
                                            (
                                                [code-type] => publisher
                                                [code-key] => sportsnetwork.com
                                            )

                                    )

                                [1] => SimpleXMLElement Object
                                    (
                                        [@attributes] => Array
                                            (
                                                [code-type] => priority
                                                [code-key] => normal
                                            )

                                    )

                                [2] => SimpleXMLElement Object
                                    (
                                        [@attributes] => Array
                                            (
                                                [code-type] => sport
                                                [code-key] => 15027000
                                            )

                                    )

                                [3] => SimpleXMLElement Object
                                    (
                                        [@attributes] => Array
                                            (
                                                [code-type] => league
                                                [code-key] => l.pga.com
                                            )

                                    )

                            )

                    )

            )

    )

)

并尝试获取

echo $scorexml->sports-content->sports-metadata['doc-id'];

但是当我尝试回复它时会出错:

  

Parse error: syntax error, unexpected T_OBJECT_OPERATOR, expecting ',' or ';'

任何帮助表示感谢。

1 个答案:

答案 0 :(得分:1)

因为元素是连字符的,所以你需要用{''}包装它们。属性很好,因为它们像数组一样被访问,比如[' doc-id']。如果它们包含连字符,则需要包装的元素。

$scorexml->{'sports-content'}->{'sports-metadata'}['doc-id']

查看示例#3: http://php.net/manual/en/simplexml.examples-basic.php