JerryAsk
JerryAsk

Reputation: 11

SimpleXMLElement Object is missing nodes

I'm using simplexml_load_string to parse XML data. Everything has been going fine, except for 1 exception where certain child nodes seem to be missing. Here's a sample of the original XML output:

<LEAGUE Name="NFL Football">
    <EVENT GameID="371667" Title="Kansas City Chiefs at Oakland Raiders" Date="2014-11-20" Time="20:25" Timezone="EST" Status="SCHEDULED" Enabled="true">
        <TOTALS Over="42.5" OverOdds="-110.0" OverEnabled="true" Under="42.5" UnderOdds="-110.0" UnderEnabled="true"/>
        <CONTESTANT Name="Kansas City" Score="0" RotationNumber="109">
            <LINE Money="-355.0" MoneyEnabled="true" Points="-7.5" Odds="-115.0" PointsEnabled="true"/>
        </CONTESTANT>
        <CONTESTANT Name="Oakland" Score="0" RotationNumber="110">
            <LINE Money="295.0" MoneyEnabled="true" Points="7.5" Odds="-105.0" PointsEnabled="true"/>
        </CONTESTANT>
    </EVENT>

Everything loads fine except for the LINE node. If I do a print_r, it displays like this (skipping to the Contestant node for brevity):

[CONTESTANT] => Array
    (
        [0] => SimpleXMLElement Object
            (
                [@attributes] => Array
                    (
                        [Name] => Kansas City
                        [Score] => 0
                        [RotationNumber] => 109
                    )

                [0] => SimpleXMLElement Object
                    (
                        [@attributes] => Array
                            (
                                [Money] => -355.0
                                [MoneyEnabled] => true
                                [Points] => -7.5
                                [Odds] => -115.0
                                [PointsEnabled] => true
                            )

                    )

            )

The data from the child node is there (Money, MoneyEnabled, etc) but it's not within the LINE node as expected. In fact, I've been unable to access this info via the SimpleXML object. Using CakePHP's debug function, the LINE data doesn't even show up. I've used this function all over the place and this is the only time any SimpleXML data doesn't display. Using CakePHP's debug:

object(SimpleXMLElement) {
@attributes => array(
    'GameID' => '371667',
    'Title' => 'Kansas City Chiefs at Oakland Raiders',
    'Date' => '2014-11-20',
    'Time' => '20:25',
    'Timezone' => 'EST',
    'Status' => 'SCHEDULED',
    'Enabled' => 'true'
)
TOTALS => object(SimpleXMLElement) {
    @attributes => array(
        'Over' => '42.5',
        'OverOdds' => '-110.0',
        'OverEnabled' => 'true',
        'Under' => '42.5',
        'UnderOdds' => '-110.0',
        'UnderEnabled' => 'true'
    )
}
CONTESTANT => array(
    (int) 0 => object(SimpleXMLElement) {
        @attributes => array(
            'Name' => 'Kansas City',
            'Score' => '0',
            'RotationNumber' => '109'
        )
    },
    (int) 1 => object(SimpleXMLElement) {
        @attributes => array(
            'Name' => 'Oakland',
            'Score' => '0',
            'RotationNumber' => '110'
        )
    }
)

As you can see, there's no LINE node under each CONTESTANT node.

Upvotes: 1

Views: 1302

Answers (1)

michi
michi

Reputation: 6625

Don't know CAKE, but don't trust print_r and for that matter var_dump() with SimpleXML.

Rather do

$xml = simplexml_load_string($x); // assume XML in $x
echo $xml->asXML();

And voilà, XML is complete, see in action: https://eval.in/224134

Upvotes: 2

Related Questions