1 Replies - 157 Views - Last Post: 31 January 2012 - 12:44 PM Rate Topic: -----

Topic Sponsor:

#1 NewToJava2011  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 92
  • Joined: 21-November 11

Solutions for parsing a single value from an XML tag

Posted 31 January 2012 - 12:31 PM

I am creating a website on currency exchange rates. Currently, I'm pulling in XML data from http://themoneyconverter.com as displayed visually below.

Attached Image

What I would like is for the user to enter a value which I shall multiply with the current exchange rate as supplied from the rss feed.

Currently, I'm just echoing out the XML tag contents as displayed below the convert button. From this example, I only wish to get the current exchange rate which happens to be 1.20482 Euro in this example which I will then multiply.

The contents of the tag is:

<description>1 British Pound Sterling = 1.20482 Euro</description>


the same that I am echoing out. Any ideas how I can access the 1.20482 value only?

Possible Solutions:

  • Is it possible to parse merely this value as oppose to everything in the XML tag?

  • Would creating a substring be an option from the code that I already have?


This is the code I'm currently using to access the above tag from the RSS feed

function get_rate1(SimpleXMLElement $xml) {
    $exchange['rate'] = $xml->channel->item[15]->description;
    echo $exchange['rate'] . "<br />";

    return $rate;
}

function get_total($exchangeRssUrl) {

// Get XML data from source
    if (isset($exchangeRssUrl)) {
        $feed = file_get_contents($exchangeRssUrl);
    } else {
        echo 'Feed not found.  Check URL';
    }

    checkFeedExists($feed); 

    $xml = new SimpleXmlElement($feed);
    $rate = get_rate1($xml);

    return $exchange;
}

//Display Total //
function displayTotal($exchangeRate1RssUrl) {

    if (isset($exchangeRate1RssUrl)) {
        echo get_total($exchangeRate1RssUrl);
        echo '<br />';
    } else {
        content_unavailable();
    }
}

// Check Feed is Available
function checkFeedExists($feed) {
    if (!$feed) {
        die('Feed not found');
    }
}



This post has been edited by NewToJava2011: 31 January 2012 - 12:32 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Solutions for parsing a single value from an XML tag

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 2146
  • View blog
  • Posts: 5,429
  • Joined: 08-June 10

Re: Solutions for parsing a single value from an XML tag

Posted 31 January 2012 - 12:44 PM

I’d probably get the text of that element and extract the number via Regular Expression.

PS. the XML is not very user friendly in that sense …
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1