SimpleXMLElement对象缺少节点

时间:2014-11-20 16:52:52

标签: php xml simplexml

我使用simplexml_load_string来解析XML数据。一切都很顺利,除了1个异常,其中某些子节点似乎缺失。这是原始XML输出的示例:

<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>

除LINE节点外,一切都很好。如果我执行print_r,它会显示如下(为了简洁起见,跳到参赛者节点):

[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
                            )

                    )

            )

来自子节点的数据存在(Money,MoneyEnabled等),但它不在预期的LINE节点内。事实上,我无法通过SimpleXML对象访问此信息。使用CakePHP的调试功能,LINE数据甚至无法显示。我已经在所有地方使用过此功能,这是任何SimpleXML数据无法显示的唯一时间。使用CakePHP的调试:

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'
        )
    }
)

如您所见,每个CONTESTANT节点下都没有LINE节点。

1 个答案:

答案 0 :(得分:2)

不知道CAKE,但不信任print_rvar_dump() SimpleXML。{/ p>

宁可做

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

瞧,XML已经完成,请参阅:https://eval.in/224134