1

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.

JerryAsk
  • 11
  • 3
  • Thanks a lot guys, that worked. Still not 100% sure why print_r() or debug() didn't work in this 1 case, but good to know. – JerryAsk Nov 20 '14 at 20:37
  • Simplexml has a lot of magic and using `print_r()` or `var_dump()` never show you the whole picture. As suggested `->asXML()` is safe. It's most likely that `debug()` has similar problems to show the XML structure, but as written, to show the XML structure, use `->asXML()` and you should be fine. If you want to learn about the rules why `print_r` for example does display it the way it does, read into what happens when you cast a **SimpleXMLElement** to an array. – hakre Nov 23 '14 at 13:23

1 Answers1

2

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

michi
  • 6,565
  • 4
  • 33
  • 56
  • var_dump shows the element as expected as well, https://eval.in/224168 – so the issue might be that var_dump is overwritten by some extension on OP’s system, or that cakephp is messing with it. – CBroe Nov 20 '14 at 19:23
  • I never actually tried var_dump on it. In all other cases, debug() worked, then I tried print_r(), and never tried a third method. – JerryAsk Nov 21 '14 at 02:53