Solved

World Bank Data API: Converting links to data?

Posted on 2015-01-01
4
129 Views
Last Modified: 2015-01-19
Hello,

The World Bank website currently allows anyone to use it's worldwide data

In the above page, the World bank provides a basic call structure format. Following this protocol, I have generated links such as:
Surface area (sq. km) of Ethiopia: http://api.worldbank.org/countries/ETH/indicators/AG.SRF.TOTL.K2?MRV=1&Gapfill=Y

Entering the above link into a url, returns the following data:

<wb:data xmlns:wb="http://www.worldbank.org" page="1" pages="1" per_page="50" total="1">
<wb:data>
<wb:indicator id="AG.SRF.TOTL.K2">Surface area (sq. km)</wb:indicator>
<wb:country id="ET">Ethiopia</wb:country>
<wb:date>2014</wb:date>
<wb:value>1104300</wb:value>
<wb:decimal>0</wb:decimal>
</wb:data>
</wb:data>

Open in new window


I'm not a programmer, but all I want to be able to do is grab the <wb:value> of 1104300 above and add it to a webpage, for multiple url links.

E.g my website would have 10 separate pages (one for each country) and on each of these pages would be the label: Surface area (sq. km): alongside the pulled World Bank data from the above url.

How would I approach this?
0
Comment
Question by:dabug80
  • 3
4 Comments
 
LVL 32

Expert Comment

by:shalomc
ID: 40529297
I didn't know that about the world bank :)

You need some code and programming to extract the value from the XML. Which language do your programmers use, or which language is used on you web site?

There are 2 general approaches:
* Do it on the server. If your web site uses ruby code, then the logic of retrieving the links, parsing the XML and displaying it in context, will be written in ruby.

* Do it on the client. You will write Javascript code that runs in the browser to retrieve the links, parse the XML and display it in context.
0
 
LVL 32

Expert Comment

by:shalomc
ID: 40529356
Just checked - you can't do it client-side in Javascript, because the CORS headers are missing on the World Bank API :(
0
 
LVL 32

Accepted Solution

by:
shalomc earned 500 total points
ID: 40529385
Here is a php snippet that does the job

 
<?php 
function httpGet($url)
{
    $ch = curl_init();  
 
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
//  curl_setopt($ch,CURLOPT_HEADER, false); 
    curl_setopt($ch,CURLOPT_ENCODING , "gzip");

    $output=curl_exec($ch);
 
    curl_close($ch);
    return $output;
}
 
 

$xmlString=httpGet("http://api.worldbank.org/countries/ETH/indicators/AG.SRF.TOTL.K2?MRV=1&Gapfill=Y"); 

$xml=simplexml_load_string( $xmlString ) or die("Error: Cannot create object");
// This gets all nodes in namespace wb that is used inside the XML file
$wb = $xml->children("wb",true);
// print_r($wb); 

echo "<p>"; 
echo "country: " . $wb->data[0]->country . "<br>";
echo "Area: " . $wb->data[0]->value; 


?>

Open in new window

0
 
LVL 1

Author Closing Comment

by:dabug80
ID: 40557419
Excellent. Thanks for taking the time to code and explain. Really appreciate it.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
In order to have all security and back ups taken care of, WordPress users can sign up for services with WP Engine.
The purpose of this video is to demonstrate how to add AdSense Ads to a WordPress Website, and how to set up WordPress to automatically place Ads in Sidebars. This will be demonstrated using a Windows 8 PC. Log into your AdSense account. : Cli…
The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now