Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

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

public class Main {
    private static String SEARCH_REQUEST_LOCATION_URL = "http://search.twitter.com/search.atom?phrase=";

    public static org.w3c.dom.Document getTweetsByKeywords(String keywords) {
        keywords = keywords.replace(' ', '+');
        return getXMLDocument(SEARCH_REQUEST_LOCATION_URL + keywords);

    }

    private static org.w3c.dom.Document getXMLDocument(String url) {

        Document document = null;

        DocumentBuilderFactory builderFactory;
        DocumentBuilder parser;

        // Try to load the document at the specified filepath into a DOM structure.
        //
        try {

            builderFactory = DocumentBuilderFactory.newInstance();

            parser = builderFactory.newDocumentBuilder();

            document = parser.parse(url);

        } catch (ParserConfigurationException p) {

            System.out.println("Error creating DOM parser.");
            System.out.println("   " + p.getMessage());

        } catch (SAXException s) {

            System.out.println("XML document returned is not well-formed.");
            System.out.println("   URL : " + url);
            System.out.println("   " + s.getMessage());

        } catch (IOException i) {

            System.out.println("Error accessing the stream.");
            System.out.println("   URL : " + url);
            System.out.println("   " + i.getMessage());
            document = null;

        }

        return document;

    }
}