willsko87 Posted February 5, 2010 Share Posted February 5, 2010 Hi, I am trying to pull some information from an xml file but I am having trouble trying to access some values. There are two <stats> sections but I can only seem to access the second one. Here is the XML file: <game_live> <stats team="h"> <player name="A Player" shortname="a" num="10" id="250365" pos="INT" k="8" h="10" m="7" t="4" ho="0" ff="2" fa="1" g="0" b="0" in50="2" rankingpoints="87" fantasypoints="80"/> </stats> <stats team="a"> <player name="B Player" shortname="b" num="44" id="220009" pos="CHB" k="14" h="12" m="6" t="1" ho="0" ff="0" fa="1" g="0" b="0" in50="1" rankingpoints="92" fantasypoints="85"/> </stats> I am trying to get the players name under the first stats section. This would be 'A Player' but I cannot work out how to get it. Below is the code I've used . <?php $xmlFileData = file_get_contents("match-10250605.xml"); $xmlData = new SimpleXMLElement($xmlFileData); foreach($xmlData->stats[0] as $stats1) { $name = $stats1->player[0]["name"]; } echo "Name: ".$name; ?> For some reason there is no output. If I put in just 'stats' instead of 'stats[0]' then it goes straight the second stats section and outputs that players name 'B Player'. What am I doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/191018-problems-parsing-xml-using-php/ Share on other sites More sharing options...
scotmcc Posted February 5, 2010 Share Posted February 5, 2010 Try doing this: $name = (String)$stats1->player[0]["name"]; Quote Link to comment https://forums.phpfreaks.com/topic/191018-problems-parsing-xml-using-php/#findComment-1007240 Share on other sites More sharing options...
willsko87 Posted February 7, 2010 Author Share Posted February 7, 2010 That doesn't do anything :-\ Still no output Quote Link to comment https://forums.phpfreaks.com/topic/191018-problems-parsing-xml-using-php/#findComment-1008305 Share on other sites More sharing options...
salathe Posted February 7, 2010 Share Posted February 7, 2010 The problem is that you're only echoing the last value found, effectively ignoring any others. With stats[0] you only target the first stats, without the [0] you target them all but only ever keep a note of the last one which is echoed outside of the loop. Change your foreach loop such that each item gets echoed within the loop, or you store an array of names for use later. foreach ($xmlData->stats as $stats1) { echo "Name: " . $stats1->player[0]["name"]; } Quote Link to comment https://forums.phpfreaks.com/topic/191018-problems-parsing-xml-using-php/#findComment-1008307 Share on other sites More sharing options...
willsko87 Posted February 7, 2010 Author Share Posted February 7, 2010 Ok that works. It ends up outputting the first 'name' from each. Thanks for that. Ultimately I would like to output the name of the players (attribute "name") in order of how many goals they have scored (attribute "g"). Any ideas how I would go about doing this? Quote Link to comment https://forums.phpfreaks.com/topic/191018-problems-parsing-xml-using-php/#findComment-1008311 Share on other sites More sharing options...
willsko87 Posted February 7, 2010 Author Share Posted February 7, 2010 Sorry to dbl post but it wouldn't let me edit it. To the last poster, how would I place both those outputted names into an array so I can call them separately? Quote Link to comment https://forums.phpfreaks.com/topic/191018-problems-parsing-xml-using-php/#findComment-1008321 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.