Get nodes from XML file not working in php

RonEskinder

I have the following XML structure, and need to show the timestamp of the < Match> and Stadium name, city and country name but my code is not working at all... any help here?

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

What i have in PHP to try and get those nodes:

<?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'];

        }
    } 
?>
helderdarocha

You can do it using XPath. It's easier to navigate in the XPath structure and get the nodes that you want. This script:

<?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";
    }
?>

using your file as input, will print this result:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Remove Nodes from Xml not working in PHP

From Dev

How to get child nodes exactly and data in that node from XML file

From Dev

PHP Get xml nodes by index

From Dev

Get Array From XML Nodes

From Dev

php get xml from remote url not a file

From Dev

Get attribute from xml file php

From Dev

php get xml from remote url not a file

From Dev

PHP - Get value from xml file

From Dev

get att value from xml file php

From Dev

Search inside XML file and get sibling nodes

From Dev

PHP Simple XML to get Nodes based on their attribute

From Dev

remove nodes listed into XmlNodeList from XML file

From Dev

PowerShell XML Nodes from Another File

From Dev

Cannot load internal nodes from XML file

From Dev

Create pairs of nodes from an XML file

From Dev

How to remove nodes from an XML file

From Dev

Delete nodes from XML file in SQL Server

From Dev

Get Nodes from xml by specifying limit

From Dev

get nested tag value from xml file using php

From Dev

get nested tag values from xml file using php

From Dev

Get information from a form and writing to an xml file using php?

From Dev

how to get values from xml file then print them using php

From Dev

Get data from an external XML file using PHP

From Dev

Inserting nodes from a XML to another XML file using xslt

From Dev

XML get nodes with sub nodes

From Dev

XML File - Get specific child nodes in unlimited node depths

From Dev

Java get XML nodes

From Dev

file_get_contents not working with php file

From Dev

How to get the names of all nodes and attributes of an xml in php?