0

I am just getting started with Simple XML

Parsing this data stored as $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
                                            )

                                    )

                            )

                    )

            )

    )

)

and trying to get

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

but get an error when I try to echo it:

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

Any help appreciated.

i alarmed alien
  • 9,412
  • 3
  • 27
  • 40
  • Can you post the XML as well. – slapyo Oct 09 '14 at 22:12
  • Please actually read the ["Basic SimpleXML usage" section in the PHP Manual](http://php.net/manual/en/simplexml.examples-basic.php). And most of SimpleXML questions have been answered already, please use the search before asking. – hakre Oct 10 '14 at 13:45

1 Answers1

1

Because the elements are hyphenated you need to wrap them with {''}. Attributes are fine since they are accessed like an array, like ['doc-id']. It's the elements that you need to wrap if they contain a hyphen.

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

Check out example #3: http://php.net/manual/en/simplexml.examples-basic.php

slapyo
  • 2,979
  • 1
  • 15
  • 24