Pages

Saturday, March 19, 2011

HTML Parser - Java Library to extract html Elements


HTML Parser is a very cool java library which can be used to extract values in a html page.

Let's take the URL - http://www.google.ca/finance?q=BMO


Let's say we want to extract the value of the span tag - ( Mar 18 - Close.)

Here is the code

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.filters.CssSelectorNodeFilter;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

public class test {

/**
* @param args
*/

public static void main(String[] args) {

String URL = "http://www.google.ca/finance?q=BMO";

try {

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

Parser parser = new Parser(urlcon); // creating of HTML parser
// object
CssSelectorNodeFilter cssFilter = new CssSelectorNodeFilter(
"SPAN.nwp");
NodeList nodes = parser.parse(cssFilter); // getting nodes of
// span.nwp. Only NodeList created with one value

Node node = nodes.elementAt(0); //getting the first value of the nodes list
System.out.println(nodes.size());
String value = node.getFirstChild().getText(); //getting the value of the node
System.out.println(value);

} catch (MalformedURLException e) {
System.out.println("Malformed Exception :" + e.getMessage());
} catch (IOException e) {

e.printStackTrace();
} catch (ParserException e) {
e.printStackTrace();
}

}
}




Saturday, December 4, 2010

MySQL - Having reserved key word as a column name

In MySql you will run into trouble when you try to execute a query if you have a reserved keyword as a column name. As an example let's take "update google_mapping set Open_Market='N' where Group='Taiwan';. Group is a reserved keyword in MySql. Solution is to surround keyword with ``. So in the above example you have to modify the query like this.
"update google_mapping set Open_Market='N' where `Group`='Taiwan'";

Saturday, October 30, 2010

Esoteric programming language

Esoteric programming language - Wikipedia, the free encyclopedia

Escoteric programming languages! Well I heard about them today for the first time. These programming languages are not used in real world programming. They have mainly used to test the boundaries of computer programming language design, as a joke and as a prof of concept by hackers and hobbyists . You can find great deal about them in this wiki article.