How to get Alexa Ranking In Java

alexa ranking

In this example, we show you how to use Java and DOM XML parser to get the Alexa ranking from below the undocumented API :


http://data.alexa.com/data?cli=10&url=domainName

1. Alexa API

For example, type following URL in your browser :


http://data.alexa.com/data?cli=10&url=mkyong.com

Alexa will return back following XML result :


<ALEXA VER="0.9" URL="mkyong.com/" HOME="0" AID="=">
<DMOZ>
<SITE BASE="mkyong.com/" 
	TITLE="J2EE Web Development" 
	DESC="Java / J2EE Web Development Tutorials, 
	which involve Spring, Hibernate, Struts, Maven, jUnit, TestNG, jQuery...">
<CATS/>
</SITE>
</DMOZ>
<SD>
<POPULARITY URL="mkyong.com/" TEXT="10720" SOURCE="panel"/>
<REACH RANK="7924"/>
<RANK DELTA="+600"/>
<COUNTRY CODE="IN" NAME="India" RANK="3542"/>
</SD>
</ALEXA>

Refer to the element “POPULARITY“, the value of “TEXT” attribute is the Alexa ranking.

2. Java, DOM and Alexa API

In Java, just send a normal HTTP request to the API, and use XML parser to get the Alexa ranking.

AlexaSEO.java

package com.mkyong.seo;

import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class AlexaSEO {

	public static void main(String[] args) {

		AlexaSEO obj = new AlexaSEO();
		System.out.println("Ranking : " + obj.getAlexaRanking("mkyong.com"));

	}

	public int getAlexaRanking(String domain) {

		int result = 0;
		
		String url = "http://data.alexa.com/data?cli=10&url=" + domain;

		try {

			URLConnection conn = new URL(url).openConnection();
			InputStream is = conn.getInputStream();
			
			DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder();
			Document doc = dBuilder.parse(is);

			Element element = doc.getDocumentElement();

			NodeList nodeList = element.getElementsByTagName("POPULARITY");
			if (nodeList.getLength() > 0) {
				Element elementAttribute = (Element) nodeList.item(0);
				String ranking = elementAttribute.getAttribute("TEXT");
				if(!"".equals(ranking)){
					result = Integer.valueOf(ranking);
				}
			}

		} catch (Exception e) {
			System.out.println(e.getMessage());
		}

		return result;
	}
}

Result :


Ranking : 10720

My website mkyong.com is ranked 10720 in Alexa, not bad.

References

  1. Fetching Alexa data
  2. How to get Google PageRank in Java
  3. How To Read XML File In Java – (DOM Parser)

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
8 Comment threads
2 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
8 Comment authors
Roman LevytskyXiao LingDev AnandSteven Mark FordJamil Recent comment authors
newest oldest most voted
Roman Levytsky
Guest
Roman Levytsky

hey guys , so i was wondering how exactly i can retrieve 100 sites by given country ? how should the url look like ?

Xiao Ling
Guest
Xiao Ling

Great post!

Dev Anand
Guest
Dev Anand

Hi, Do you have url & xml format to get top 10 sites in particular country?

Steven Mark Ford
Guest
Steven Mark Ford

lol@”My website mkyong.com is ranked 10720 in Alexa, not bad.” a bit of an understatement there yong! :) Good Job. I was going to post an article on how to do this in Google app engine + python etc.

Jamil
Guest
Jamil

Nice tutorial . Thankyou :)

Srihari Thalla
Guest
Srihari Thalla

Thanks :)

Srihari Thalla
Guest
Srihari Thalla
Vivek
Guest
Vivek

I have tried this example in my local ,its giving me ranked 0.Please explain why its happening…

Gowtham Gutha
Guest
Gowtham Gutha

Your site might probably be a new one or it is not ranked by Google till the date.

trackback
How to read XML file in Java – (DOM Parser)

[…] You may interest at this How to get Alexa Ranking In Java. It shows you how to use DOM to parse the Alexa XML […]