访问SimpleXML属性


accessing SimpleXML attribute

我刚刚开始使用简单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 ';'

感谢您的帮助。

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

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

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