Monday, 28 January 2013

Get the website ranking using Alexa API in Java

Using DOM Parser and Alexa API
You have to send a normal HTTP request to the Alexa API, and by using DOM XML parser to get the Alexa ranking of any web site. Here I am referring to my own web site and the ranking is not so good :). Refer the below code snippet for your reference.

package com.sanjeetpandey;

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 AlexaRanking {
 
	public static void main(String[] args) {
 
	AlexaSEO obj = new AlexaSEO();
	System.out.println("Ranking is ::: " + obj.getAlexaRanking("sanjeetpandey.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;
}
}

Output -
Ranking is ::: 123123

Construct File Path In Java

Examples to construct a file path in Java :

Identify the operating system and create the file separator manually. (Not recommend)
Let’s Java do all the job, i.e File.separator. (Best Practice)
File.separator is always recommended, because it will check your OS and display the correct file separator automatically, and also you can use this best practice to reduce the code. For example,

Windows – Return “\”
*nix – Return “/”.

Manual file separator example
Way to construct a file path manually, which is not recommended in programming world.

package com.sanjeetpandey;
 
import java.io.File;
import java.io.IOException;
 
public class FilePath 
{
    public static void main( String[] args )
    { 
     try {
 
       String filename = "test.txt";
       String finalPath = "";
       String workingDir = System.getProperty("user.dir");
 
       String osName = System.getProperty("os.name").toLowerCase();
       if(osName.indexOf("win") >= 0){
        finalfile = workingDir + "\\" + filename;
       }else if(osName.indexOf( "nix") >=0 || your_os.indexOf( "nux") >=0){
        finalfile = workingDir + "/" + filename;
       }else{
        finalfile = workingDir + "{others}" + filename;
       }
 
       System.out.println("Final filepath : " + finalPath);
       File file = new File(finalPath);
 
   if (file.createNewFile()){
      System.out.println("Done");
   }else{
      System.out.println("File already exists!");
   }
 
     } catch (IOException e) {
      e.printStackTrace();
 }
    }
}

File.separator example
A proper way is use the File.separator, see the different below, just one line code can do all the checking above.

package com.sanjeetpandey;
 
import java.io.File;
import java.io.IOException;
 
public class FilePath 
{
    public static void main( String[] args )
    { 
     try {
 
       String filename = "test.txt";
       String finalPath = "";
       String workingDir = System.getProperty("user.dir");
 
       finalPath = workingDir + File.separator + filename;
 
       System.out.println("Final filepath : " + finalPath);
       File file = new File(finalPath);
 
   if (file.createNewFile()){
      System.out.println("Done");
   }else{
      System.out.println("File already exists!");
   }
 
     } catch (IOException e) {
      e.printStackTrace();
 }
    }
}