Thursday, April 17, 2014

Get a list of Countries and Capitals of the World

Here is a simple snippet to get the countries of the world and its corresponding capitals, again via XMLHttpRequest. And so the result is dependent whether the source site won't change their HTML codes.

Actually, this is just a proposed answer to a member in need inside Foxite as he is looking for a way to get a list of something like this.  I surfed the net a while ago and saw a good site to extract info from and since there is a possibility that  someone out there who is not a member of the forum may need something like this, then here it is:

****************
* Get Countries & Capitals
****************


* For Clearing URL Cache
Declare Integer DeleteUrlCacheEntry In wininet String lpszUrlName

Local o As msxml2.xmlhttp, lcURL
lcURL = [http://www.mapsofworld.com/countries-capitals/]
o = Createobject("msxml2.xmlhttp")

o.Open("GET",m.lcURL,.F.)
o.Send(.Null.)
Do While o.readyState <> 4
      Wait Window Seconds() Nowait
Enddo
Wait Window 'Getting Info, please wait....' Nowait

Create Cursor Countries (Country c(50), Capital c(40))
lcSection = Strextract(o.responseText,'>Capital<','</table>')
For lnloop = 1 To 500
      Try
            Insert Into Countries (Country) Values (Strextract(m.lcSection,'<td>','</td>',m.lnloop))
            Replace Capital With (Strextract(m.lcSection,'<td>','</td>',m.lnloop+1))
            lnloop = lnloop + 1
      Catch
            Exit
      Endtry
Next

* Sort it now, remove empty extractions
Select * From Countries Where !Empty(Country) Order By 1 Into Cursor Countries NOFILTER

Browse Normal

o.abort
DeleteUrlCacheEntry(m.lcURL)

Enjoy!

1 comment: