Jump to content

Simple XML Help


jadedknight

Recommended Posts

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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++;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.