728x90 AdSpace

Saturday, 4 January 2014

How to get Alexa Page Rank using Java Program

In this tutorial we are going to see how to get Alexa page rank using Java program
enter the following URL in your web browser
http://data.alexa.com/data?cli=10&url=javatutorialscorner.com
It will return the following result
<ALEXA VER="0.9" URL="javatutorialscorner.com/" HOME="0" AID="=" IDN="javatutorialscorner.com/">
<DMOZ>
<SITE BASE="javatutorialscorner.com/" TITLE="Java Tutorials Corner" DESC="Java Tutorials Corner|Java Tutorial|J2EE Tutorial |Servlet Tutorial|Jsp Tutorial|Web Service Tutorial|Struts 1.x tutoria|Struts 2.x Tutorial|Hibernate 3.6 Tutorial|Hibernate 4 Tutorial|Struts Tutorial|Hibernate Tutorials|Spring Tutorial|Jdbc Tutorial|Video Tutorial|Sql Tutorial|MySql Tutorial|Sql Server Tutorial|Html Tutorial|Java Script Tutorial|Jquery Tutorial|CSS Tutorial|XML Tutorial|YUI 2.9 Tutorial|YUI 3.0 Tutorials|YUI Tutorial|JSF Tutorial|RMI Tutorial|EJB Tutorial|JPA Tutorial|JavaFX Tutorial|Log4J Tutorial|Android Tutorial|AJAX Tutorial|JAXB Tutorial|Tomcat Tutorials |apache Ant">
<CATS/>
</SITE>
</DMOZ>
<SD>
<POPULARITY URL="javatutorialscorner.com/" TEXT="1289346" SOURCE="panel"/>
<REACH RANK="1541504"/>
<RANK DELTA="+290779"/>
<COUNTRY CODE="IN" NAME="India" RANK="168712"/>
</SD>
</ALEXA>

In above XML the POPULARITY tag contains TEXT attribute it is the page rank of site.
Now we will see how to get this in Java Program

AlaxaPageRank.java

package com.javatutorialscorner.java;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class AlaxaPageRank {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String alaxaURI = "http://data.alexa.com/data?cli=10&url=";
String domain = "javatutorialscorner.com";

final String SITE_POPULARITY = "POPULARITY";
final String RANK = "TEXT";
try {
domain = alaxaURI + domain;

URLConnection connection = new URL(domain).openConnection();
InputStream inputStream = connection.getInputStream();
DocumentBuilder builder = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
Document document = builder.parse(inputStream);

Element element = document.getDocumentElement();
NodeList nodeList = element.getElementsByTagName(SITE_POPULARITY);
if (nodeList != null && nodeList.getLength() > 0) {

Element element2 = (Element) nodeList.item(0);
String pageRank = element2.getAttribute(RANK);

if (!"".equals(pageRank)) {
System.out.println("Site Rank : " + pageRank);
} else {
System.out.println("Your web site not indexed");
}

} else {
System.out.println("Your web site not indexed");
}

} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

In above program sends the HTTP request and parse the XML  response.You can see the following output in console

Site Rank : 1289346
Annamalai Thangaraj

Annamalai is working as Technical Lead in Leading Telecom company with 5+ years experience in Identity and Access Management , Telecom and Networks, BigData, Java, Spring, Struts, Hibernate, AngularJS, and Enterprise Web Application Development.

Website: Java Tutorials Corner

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: How to get Alexa Page Rank using Java Program Rating: 5 Reviewed By: Annamalai Thangaraj