jadedknight Posted July 9, 2010 Share Posted July 9, 2010 So I want to parse through this XML: http://pastebin.com/afPrzED1 I want to echo every realm name. I can get the first set of realm names in the first <Battlegroup> block, but I cannot get past those to the others. I'm not really sure how to move on.. maybe child node or something? I have no clue haha. Can anyone help? Below I pasted some ideas I was just messing around with. $armory_xml = simplexml_load_file($url); echo $armory_xml->battlegroups->battlegroup[0]['ladderUrl']; foreach ($armory_xml->battlegroups->battlegroup as $bg_name) { //echo $bg_name[0]['ladderUrl']; foreach ($armory_xml->battlegroups->battlegroup->realms as $realms) { foreach($armory_xml->battlegroups->battlegroup->realms->realm as $realm){ echo $realm[0]['nameEN']; } } } print_r($armory_xml); $armory_xml->battlegroups->battlegroup->realms->realm[0]->attributes() as $realm => $realm_name foreach ($armory_xml->battlegroups->battlegroup as $battlegroup) { $i = $battlegroup[0]->['ladderUrl']; echo $i; foreach ($armory_xml->battlegroups->battlegroup->realms->realm as $realm) { echo $realm[0]->attributes() . " "; } } foreach ($armory_xml->battlegroups->battlegroup as $bg_name) { echo $bg_name->realms->realm[0]['nameEN']; } foreach ($armory_xml->battlegroups->battlegroup as $bg_name) { echo $bg_name[2]->realms->realm[4]['nameEN']; } Quote Link to comment https://forums.phpfreaks.com/topic/207259-simple-xml-help/ Share on other sites More sharing options...
Wolphie Posted July 9, 2010 Share Posted July 9, 2010 I managed to get it working using this code: <?php $filename = '/path/to/battlegroups.xml'; if (file_exists($filename)) { $xml = simplexml_load_file($filename); // Print battlegroups foreach ($xml->battlegroups->battlegroup as $battlegroup) { echo '<h1>' . $battlegroup['display'] . '</h1>'; // Print realms for each battlegroup echo '<ul>'; foreach ($battlegroup->realms->realm as $realm) { echo '<li>' . $realm['name'] . '</li>'; } echo '</ul>'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/207259-simple-xml-help/#findComment-1083660 Share on other sites More sharing options...
jadedknight Posted July 9, 2010 Author Share Posted July 9, 2010 Hey thanks! I will take your simplicity and try and work with the rest of the xml for my project Quote Link to comment https://forums.phpfreaks.com/topic/207259-simple-xml-help/#findComment-1083789 Share on other sites More sharing options...
jadedknight Posted July 9, 2010 Author Share Posted July 9, 2010 I'm actually going to re-open this with another problem. Here is some more XML I am trying to parse http://pastebin.com/YD7MaiTM Here is my attempt: $url = "http://www.wowarmory.com/arena-ladder.xml?p=1&ts=2&b=Vindication"; ini_set("user_agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1"); $armory_xml = simplexml_load_file($url); $page_count = $armory_xml->arenaLadderPagedResult['page']; while ($armory_xml->arenaLadderPagedResult['page'] <= $armory_xml->arenaLadderPagedResult['maxPage']) { $arena_url = "http://www.wowarmory.com/arena-ladder.xml?p=" . $page_count . "&ts=2&b=Vindication"; $arena_xml = simplexml_load_file($arena_url); foreach ($arena_xml->arenaLadderPagedResult->arenaTeams->arenaTeam as $arena_team) { if ($arena_team['rating'] < 2000) { break; } else { echo '<p>' . $arena_team['teamUrl'] . '</p>'; } } $page_count + 1; } The results are paged, so I am trying to go through each page up to a certain point (2000 "team rating"). The problem is, this isn't throwing any errors, so I am not quite sure where I'm going wrong. How could I implement a test(?) to throw some errors. Also, I don't want to get banned because I'm calling so many URLS, how can I implement a wait time? I really appreciate all the help. Reading over it I see some flaws in my logic, just trying to wrap my head around how to page through the results and get what I want. Quote Link to comment https://forums.phpfreaks.com/topic/207259-simple-xml-help/#findComment-1083824 Share on other sites More sharing options...
jadedknight Posted July 9, 2010 Author Share Posted July 9, 2010 Umm sorry for adding another reply but it won't let me edit my posts.. tried relogging too. Anyways I updated my PHP slightly: $url = "http://www.wowarmory.com/arena-ladder.xml?p=1&ts=2&b=Vindication"; ini_set("user_agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1"); $armory_xml = simplexml_load_file($url); $page_count = $armory_xml->arenaLadderPagedResult['page']; $max_page = $armory_xml->arenaLadderPagedResult['maxPage']; while ($page_count <= $max_page) { $arena_url = "http://www.wowarmory.com/arena-ladder.xml?p=" . $page_count . "&ts=2&b=Vindication"; $arena_xml = simplexml_load_file($arena_url); foreach ($arena_xml->arenaLadderPagedResult->arenaTeams->arenaTeam as $arena_team) { if ($arena_team['rating'] < 2500) { break; } else { echo '<p>' . $arena_team['teamUrl'] . '</p>'; } } $page_count++; } I changed a few things including the "rating" to stop at since I don't have a wait time implemented yet. Quote Link to comment https://forums.phpfreaks.com/topic/207259-simple-xml-help/#findComment-1083841 Share on other sites More sharing options...
jadedknight Posted July 10, 2010 Author Share Posted July 10, 2010 Fixed it myself. If anyone is interested I'll post the solution below: $url = "http://www.wowarmory.com/arena-ladder.xml?p=1&ts=2&b=Vindication"; ini_set("user_agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1"); $armory_xml = simplexml_load_file($url); $page_count = intval($armory_xml->arenaLadderPagedResult['page']); $max_page = intval($armory_xml->arenaLadderPagedResult['maxPage']); echo $page_count . '<br />' . $max_page; while ($page_count <= $max_page) { $arena_url = "http://www.wowarmory.com/arena-ladder.xml?p=$page_count&ts=2&b=Vindication"; $arena_xml = simplexml_load_file($arena_url); foreach ($arena_xml->arenaLadderPagedResult->arenaTeams->arenaTeam as $arena_team) { if ($arena_team['rating'] < 2500) { break; } else { echo '<p>' . $arena_team['teamUrl'] . '</p>'; } } $page_count++; } Quote Link to comment https://forums.phpfreaks.com/topic/207259-simple-xml-help/#findComment-1083865 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.