java

Parse Alexa API XML Response

· John Doe

899 Views

1. Send a request to the following Alexa API.

https://data.alexa.com/data?cli=10&url=phenomena.com

2. The Alexa API will return the following XML response. The Alexa ranking is inside the POPULARITY element, the TEXT attribute.

<!--  Need more Alexa data?  Find our APIs here: https://aws.amazon.com/marketplace/seller-profile?id=4a9dbf38-88b1-4e87-a459-271154a77d2e  -->
<ALEXA VER="0.9" URL="phenomena.com/" HOME="0" AID="=" IDN="phenomena.com/">
<SD>
<POPULARITY URL="phenomena.com/" TEXT="3713994" SOURCE="panel"/>
<REACH RANK="3404850"/>
<RANK DELTA="+652325"/>
</SD>
</ALEXA>

3. We use a DOM parser to directly select the POPULARITY element and print out the value of the TEXT attribute.

package com.bad.blood.test;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

public class ReadXmlAlexaApi {
	
	private static final String ALEXA_API = "http://data.alexa.com/data?cli=10&url=";
    private final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    public static void main(String[] args) {

        ReadXmlAlexaApi obj = new ReadXmlAlexaApi();
        int alexaRanking = obj.getAlexaRanking("phenomena.com");

        System.out.println("Ranking: " + alexaRanking);

    }

    public int getAlexaRanking(String domain) {

        int result = 0;

        String url = ALEXA_API + domain;

        try {

            URLConnection conn = new URL(url).openConnection();

            try (InputStream is = conn.getInputStream()) {

                // unknown XML better turn on this
                dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);

                DocumentBuilder dBuilder = dbf.newDocumentBuilder();

                Document doc = dBuilder.parse(is);

                Element element = doc.getDocumentElement();

                // find this tag "POPULARITY"
                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.parseInt(ranking);
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            throw new IllegalArgumentException("Invalid request for domain : " + domain);
        }

        return result;
    }

}

The domain phenomena.com ranked 3713994.

Ranking: 3713994

ref. https://mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/

XML