0

I have this XML I am trying to parse: http://rss.desura.com/games/feed/rss.xml?cache=sale

Inside it has this:

<saleprices>
<price currency="USD">6.79</price>
<price currency="AUD">6.79</price>
<price currency="EUR">5.09</price>
<price currency="GBP">4.42</price>
</saleprices>

Normally using simplexml in php for a tag you would use something like $game->{'price'} for example, but how do I pick out specific ones like USD from that list? i haven't worked with an XML that has strings inside a tag before.

I am loading and reading the XML like this:

$url = 'http://rss.desura.com/games/feed/rss.xml?cache=sale';
$xml = simplexml_load_string(file_get_contents($url));

foreach ($xml->browse->game as $game)
{

1 Answer 1

4
<?php

$url = 'http://rss.desura.com/games/feed/rss.xml?cache=sale';
$xml = simplexml_load_string(file_get_contents($url));

foreach ($xml->browse->game as $game)
{
    foreach($game->saleprices->price->attributes() as $a => $b) {
       echo "Price: " . $b . " ";
       echo $game->saleprices->price . "<br />";
    }
}

will output:

Price: USD 7.99
Price: USD 2.49
Price: USD 13.99
Price: USD 4.99
......

Or get every currency and the price like this:

<?php

$url = 'http://rss.desura.com/games/feed/rss.xml?cache=sale';
$xml = simplexml_load_string(file_get_contents($url));

foreach ($xml->browse->game as $game)
{
    echo "<br />Prices:<br />";
    foreach($game->saleprices->price as $a) {
        foreach($a->attributes() as $b => $c) {
           echo $c . " ";
           echo $a . "<br />";
        }
    }
}

will output:

Prices:
USD 7.99
AUD 7.99
EUR 7.99
GBP 7.99

Prices:
USD 2.49
AUD 2.99
EUR 1.99
GBP 1.75

Prices:
USD 13.99
AUD 15.49
EUR 9.99
GBP 8.49
Sign up to request clarification or add additional context in comments.

5 Comments

What if i want to set them to usd, gbp etc? To use each of them not just USD? That seems hard-coded for only using one of them?
Take a look at my answer again, edited to print them all
That's interesting, so if i wanted to set them to a string like $usd = 7.99 then i could do a counter and count up to 4 and set them accordingly? Is that the easiest way?
You could create an array like this: "USD" => "7.99", "AUD" => "7,99", and so on. Think that'll be the easiest way to use it on your website.
How would I create that array, I can't seem to get it to work?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.