Reputation: 67
I'm trying to retrieve data from an XML document that looks like:
<scores sport="soccer" updated="08.05.2018 09:39:42">
<category name="Argentina: Superliga" gid="1081" id="1081" file_group="argentina" iscup="False">
<matches date="May 08" formatted_date="08.05.2018">
<match status="FT" timer="" date="May 08" formatted_date="08.05.2018" time="00:15" commentary_available="argentina" venue="Estadio Brigadier General Estanislao López" v="60" static_id="2288469" fix_id="2172208" id="2442089">
<localteam name="Colon Santa FE" goals="0" id="5947"/>
<visitorteam name="River Plate" goals="0" id="6042"/>
<events>
<event type="yellowcard" minute="39" extra_min="" team="visitorteam" player="C. Mayada" result="" playerId="91623" assist="" assistid="" eventid="24420891"/>
<event type="yellowcard" minute="40" extra_min="" team="localteam" player="M. Fritzler" result="" playerId="14256" assist="" assistid="" eventid="24420892"/>
<event type="subst" minute="46" extra_min="" team="visitorteam" player="J. Quintero" result="" playerId="74510" assist="G. Martinez" assistid="51346" eventid="24420893"/>
<event type="subst" minute="63" extra_min="" team="visitorteam" player="L. Pratto" result="" playerId="17657" assist="R. Mora" assistid="55877" eventid="24420894"/>
<event type="subst" minute="63" extra_min="" team="localteam" player="N. Leguizamon" result="" playerId="443149" assist="C. Bernardi" assistid="186181" eventid="24420895"/>
<event type="subst" minute="70" extra_min="" team="localteam" player="P. Ledesma" result="" playerId="13875" assist="A. Bastia" assistid="15082" eventid="24420896"/>
<event type="subst" minute="73" extra_min="" team="visitorteam" player="R. Borre" result="" playerId="320836" assist="I. Scocco" assistid="15172" eventid="24420897"/>
<event type="subst" minute="82" extra_min="" team="localteam" player="L. Heredia" result="" playerId="337735" assist="A. Ruiz" assistid="189511" eventid="24420898"/>
<event type="yellowcard" minute="90" extra_min="1" team="visitorteam" player="R. Borre" result="" playerId="320836" assist="" assistid="" eventid="24420899"/>
</events>
<ht score="[0-0]"/>
<ft score="[0-0]"/>
</match>
</matches>
</category>
<category name="Argentina: Primera B Nacional - Promotion - Play Offs" gid="3899" id="1078" file_group="argentina" iscup="False">
<matches date="May 08" formatted_date="08.05.2018">
<match status="23:05" timer="" date="May 08" formatted_date="08.05.2018" time="23:05" commentary_available="" venue="" v="0" static_id="2369960" fix_id="2253925" id="2443571">
<localteam name="Almagro" goals="?" id="5900"/>
<visitorteam name="Agropecuario" goals="?" id="21385"/>
<events/>
<ht score=""/>
</match>
</matches>
</category>
</scores>
What I have tried so far:
$xml=simplexml_load_string($content);
//print_r($xml);
foreach ($xml->scores->category->matches->match as $value) {
echo (string)$value;
}
Message I'm getting:
Notice: Trying to get property of non-object in C:\xampp\htdocs\proj\inc\get.php on line 23
Notice: Trying to get property of non-object in C:\xampp\htdocs\proj\inc\get.php on line 23
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\proj\inc\get.php on line 23
To be specific, I'm trying to extract what's inside the attributes like Date, team name, goals, etc.
Thank you in advance.
Upvotes: 0
Views: 45
Reputation: 57121
When you load an XML file using SimpleXML, the root element is the starting point for any access. So you don't need scores
as this is $xml
.
foreach ($xml->category->matches->match as $value) {
echo (string)$value['status'];
}
This outputs the status element (FT
) to show it's actually the <match>
element.
Update: As Syscall mentioned, the structure of this XML would need something like...
foreach ($xml->category as $value1) {
foreach ( $value1->matches->match as $value) {
echo (string)$value['status'];
}
}
to fetch all of the <match>
elements.
Upvotes: 1