首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >访问SimpleXML属性

访问SimpleXML属性
EN

Stack Overflow用户
提问于 2014-10-09 22:08:36
回答 1查看 331关注 0票数 0

我刚刚开始使用简单的XML。

解析存储为$scorexml的数据

代码语言:javascript
复制
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
                                            )

                                    )

                            )

                    )

            )

    )

)

试着得到

代码语言:javascript
复制
echo $scorexml->sports-content->sports-metadata['doc-id'];

但是,当我试图回显它时,会得到一个错误:

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

任何帮助都很感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-09 22:16:24

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

代码语言:javascript
复制
$scorexml->{'sports-content'}->{'sports-metadata'}['doc-id']

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26288735

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档