从XML文件获取节点不能在php中工作

时间:2014-05-26 22:10:49

标签: php xml xmlnode

我有以下XML结构,需要显示<的时间戳。匹配>和体育场名称,城市和国家名称,但我的代码根本不起作用...这里有任何帮助吗?

<?xml version="1.0" encoding="utf-8"?>
<afpdb lang="fr-FR">
    <head>
        <message type="203" file="s4133-0000000-203-fr" timestamp="2014-04-16T12:16:26+02:00" />
    </head>
    <body>
        <competition id="866" label="Brésil 2014">
            <discipline code="FB" name="Football">
                <evt id="4133" label="Brésil 2014" gender="M" date="2014-06-12T00:00:00-03:00">
                    <country iso="BRA" code="BRA" name="Brésil" />
                    <phase id="2717" code="TPFIN" type="PH1PT">
                        <group id="9296">
                            <match id="133114" num="63" status="EMNCO" day="1" timestamp="2014-07-12T17:00:00-03:00" dow="samedi" utc="2014-07-12T20:00:00+00:00">
                                <datas>
                                    <stadium id="4499" name="Stade national Mané Garrincha">
                                        <city id="2460" name="Brasilia">
                                            <country iso="BRA" code="BRA" name="Brésil" />
                                        </city>
                                    </stadium>
                                </datas>
                                <res pos="1">
                                    <team type="CETAB" display="NC" />
                                </res>
                                <res pos="2">
                                    <team type="CETAB" display="NC" />
                                </res>
                            </match>
                        </group>
                        <group id="9297">
                            <match id="133115" num="64" status="EMNCO" day="1" timestamp="2014-07-13T16:00:00-03:00" dow="dimanche" utc="2014-07-13T19:00:00+00:00">
                                <datas>
                                    <stadium id="172" name="Stade Maracana">
                                        <city id="101" name="Rio de Janeiro">
                                            <country iso="BRA" code="BRA" name="Brésil" />
                                        </city>
                                    </stadium>
                                </datas>
                                <res pos="1">
                                    <team type="CETAB" display="NC" />
                                </res>
                                <res pos="2">
                                    <team type="CETAB" display="NC" />
                                </res>
                            </match>
                        </group>
                    </phase>
                    <phase id="2716" code="TPSFI" type="PH1PT">
                        <group id="9294">
                            <match id="133112" num="61" status="EMNCO" day="1" timestamp="2014-07-08T17:00:00-03:00" dow="mardi" utc="2014-07-08T20:00:00+00:00">
                                <datas>
                                    <stadium id="4412" name="Estadio Mineirão">
                                        <city id="82" name="Belo Horizonte">
                                            <country iso="BRA" code="BRA" name="Brésil" />
                                        </city>
                                    </stadium>
                                </datas>
                                <res pos="1">
                                    <team type="CETAB" display="NC" />
                                </res>
                                <res pos="2">
                                    <team type="CETAB" display="NC" />
                                </res>
                            </match>
                        </group>
                        <group id="9295">
                            <match id="133113" num="62" status="EMNCO" day="1" timestamp="2014-07-09T17:00:00-03:00" dow="mercredi" utc="2014-07-09T20:00:00+00:00">
                                <datas>
                                    <stadium id="3040" name="Arena de São Paulo">
                                        <city id="107" name="Sao Paulo">
                                            <country iso="BRA" code="BRA" name="Brésil" />
                                        </city>
                                    </stadium>
                                </datas>
                                <res pos="1">
                                    <team type="CETAB" display="NC" />
                                </res>
                                <res pos="2">
                                    <team type="CETAB" display="NC" />
                                </res>
                            </match>
                        </group>
                    </phase>
                    </evt>
            </discipline>
        </competition>
    </body>
</afpdb>

我在PHP中尝试获取这些节点:

<?php 
    $document = new DOMDocument(); 
    $document->load( "s4133-0000000-203-fr.xml" ); 

    $phases = $document->getElementsByTagName( "phase" ); 

    foreach( $phases as $phase ){ 
        $groups = $phase->getElementsByTagName( "group" ); 

        foreach( $groups as $group ){
            $datas = $group->getElementsByTagName( "datas" ); 

            echo $datas->stadium['name'];
            echo $datas->stadium->city['name'];
            echo $datas->stadium->city->country['name'];

        }
    } 
?>

1 个答案:

答案 0 :(得分:1)

您可以使用XPath来完成。在XPath结构中导航并获取所需的节点更容易。这个脚本:

<?php 
    $document = new DOMDocument(); 
    $document->load( "s4133-0000000-203-fr.xml" ); 

    $xpath = new DOMXpath($document);

    foreach ($xpath->evaluate("//datas") as $datas) {
        $stadium = $xpath->evaluate("string(stadium/@name)", $datas);
        $city    = $xpath->evaluate("string(stadium/city/@name)", $datas);
        $country = $xpath->evaluate("string(stadium/city/country/@name)", $datas);
        $timestamp = $xpath->evaluate("string(parent::match/@timestamp)", $datas);

        echo $timestamp."\n    ".$stadium.", ".$city.", ".$country."\n";
    }
?>

使用您的文件作为输入,将打印此结果:

2014-07-12T17:00:00-03:00
    Stade national Mané Garrincha, Brasilia, Brésil
2014-07-13T16:00:00-03:00
    Stade Maracana, Rio de Janeiro, Brésil
2014-07-08T17:00:00-03:00
    Estadio Mineirão, Belo Horizonte, Brésil
2014-07-09T17:00:00-03:00
    Arena de São Paulo, Sao Paulo, Brésil