-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathJavaAlexaRankingEx.java
More file actions
50 lines (34 loc) · 1.56 KB
/
JavaAlexaRankingEx.java
File metadata and controls
50 lines (34 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.zetcode;
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 JavaAlexaRankingEx {
public static void main(String[] args) throws MalformedURLException,
IOException, ParserConfigurationException, SAXException {
String webSite = "something.com";
int ranking = 0;
String url = String.format("http://data.alexa.com/data?cli=10&url=%s", webSite);
URLConnection conn = new URL(url).openConnection();
try (InputStream is = conn.getInputStream()) {
DocumentBuilder builder = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
Document doc = builder.parse(is);
Element element = doc.getDocumentElement();
NodeList nodeList = element.getElementsByTagName("POPULARITY");
if (nodeList.getLength() > 0) {
Element elementAttribute = (Element) nodeList.item(0);
ranking = Integer.valueOf(elementAttribute.getAttribute("TEXT"));
}
}
System.out.printf("Ranking of %s: %d%n", webSite, ranking);;
}
}