访问SimpleXML属性

埃德·帕特曼

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章