googlexx Posted February 24, 2011 Share Posted February 24, 2011 I've been reading parsing xml with php forums all day and still can't seem to figure out how to start with this one. there is a node here: http://xml.heroesofnewerth.com/xml_requester.php?f=match_stats&opt=mid&mid[]=30428528 called match_stats. All i need help with is how to get those 10 elements inside match_stats and how to grab the stat name= nickname from each element. I'm just looking for somewhere to start and I'm sure I can figure out the rest. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/228673-parsing-xml/ Share on other sites More sharing options...
PFMaBiSmAd Posted February 24, 2011 Share Posted February 24, 2011 You would use an xpath query. Assuming you have read that URL into a file doc.xml - <?php $xml = simplexml_load_file('doc.xml'); $result = $xml->xpath("//stat[@name='nickname']"); // Selects all the stat elements that have an attribute named 'name' with a value of 'nickname' foreach($result as $node){ echo "Nickname: $node<br />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/228673-parsing-xml/#findComment-1178995 Share on other sites More sharing options...
googlexx Posted February 24, 2011 Author Share Posted February 24, 2011 You would use an xpath query. Assuming you have read that URL into a file doc.xml - How would I go about this? Quote Link to comment https://forums.phpfreaks.com/topic/228673-parsing-xml/#findComment-1179017 Share on other sites More sharing options...
googlexx Posted February 24, 2011 Author Share Posted February 24, 2011 nevermind i figured it out. for syntax issues is there a better way to grab more nodes than this? $xml = simplexml_load_file('http://xml.heroesofnewerth.com/xml_requester.php?f=match_stats&opt=mid&mid[]=30428528'); $i = 0; $result = $xml->xpath("//stat[@name='nickname']"); // Selects all the stat elements that have an attribute named 'name' with a value of 'nickname' foreach($result as $node){ $name_array[$i] = "$node"; $i++; } $i=0; $result = $xml->xpath("//stat[@name='level']"); // Selects all the stat elements that have an attribute named 'name' with a value of 'nickname' foreach($result as $node){ $level_array[$i] = "$node"; $i++; } Quote Link to comment https://forums.phpfreaks.com/topic/228673-parsing-xml/#findComment-1179022 Share on other sites More sharing options...
PFMaBiSmAd Posted February 24, 2011 Share Posted February 24, 2011 What's your overall goal and the desired output/result? Because programming is an exact science (computers only do exactly what their code and data tell them to do), when you don't take into account everything you are trying to accomplish (i.e. define what you are trying to achieve first before you write any code), you end up wasting time rewriting your code and redefining your data structures or you end up with an inefficient kludge of code that is hard to read, troubleshoot, and fix. The solution to: "how to get those 10 elements inside match_stats and how to grab the stat name= nickname from each element" can be completely different from: "I want to get the following fields for each team and display them like so _________ and/or insert them into a database like so _________ and/or calculate/sort them like so _________". Quote Link to comment https://forums.phpfreaks.com/topic/228673-parsing-xml/#findComment-1179042 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.