Thursday, February 19, 2009

HTTP Get Request and XML parsing

Below is a sample code for processing a HTTP GET request .
The below code will use the google search API for searching a word 'lotus notes'.
The result will be a XML string which will be parsed using ParseMe function




import java.net.*;
import java.io.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import javax.xml.parsers.*;
import org.xml.sax.InputSource;
import org.w3c.dom.*;
import java.io.*;

public class CopyOfURLReader {
public static void main(String[] args) throws Exception {
URL yahoo = new URL("http://books.google.com/books/feeds/volumes?q=lotus+notes&start-index=1&max-results=20");
BufferedReader in = new BufferedReader(
new InputStreamReader(
yahoo.openStream()));

String inputLine;
String finalLine="";
while ((inputLine = in.readLine()) != null)
{
System.out.println(inputLine);
finalLine += inputLine;
}
in.close();
System.out.println(finalLine);

// Parse the file using the handler
parseME(finalLine);

}

//Parsing the result

public static void parseME(String xmlRecords) {


try {
DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xmlRecords));

Document doc = db.parse(is);
NodeList nodes = doc.getElementsByTagName("entry");
NodeList name = null;
// iterate the entries
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);

if (node.getNodeType() == Node.ELEMENT_NODE)
{
Element element = (Element) node;

NodeList nameNode = element.getElementsByTagName("dc:creator");
for(int iIndex=0; iIndex< nameNode.getLength(); iIndex++)
{
if (nameNode.item(iIndex).getNodeType() ==Node.ELEMENT_NODE)
{
Element nameElement = (Element) nameNode.item(iIndex);
System.out.println("Name = " +nameElement.getFirstChild().getNodeValue().trim());
}
}
nameNode = element.getElementsByTagName("dc:date");
for(int iIndex=0; iIndex< nameNode.getLength(); iIndex++)
{
if (nameNode.item(iIndex).getNodeType() ==Node.ELEMENT_NODE)
{
Element nameElement = (Element) nameNode.item(iIndex);
System.out.println("Date = " +nameElement.getFirstChild().getNodeValue().trim());
}
}
nameNode = element.getElementsByTagName("dc:description");
for(int iIndex=0; iIndex< nameNode.getLength(); iIndex++)
{
if (nameNode.item(iIndex).getNodeType() ==Node.ELEMENT_NODE)
{
Element nameElement = (Element) nameNode.item(iIndex);
System.out.println("Description = " +nameElement.getFirstChild().getNodeValue().trim());
}
}
nameNode = element.getElementsByTagName("dc:format");
for(int iIndex=0; iIndex< nameNode.getLength(); iIndex++)
{
if (nameNode.item(iIndex).getNodeType() ==Node.ELEMENT_NODE)
{
Element nameElement = (Element) nameNode.item(iIndex);
System.out.println("Format = " +nameElement.getFirstChild().getNodeValue().trim());
}
}
nameNode = element.getElementsByTagName("dc:identifier");
for(int iIndex=0; iIndex< nameNode.getLength(); iIndex++)
{
if (nameNode.item(iIndex).getNodeType() ==Node.ELEMENT_NODE)
{
Element nameElement = (Element) nameNode.item(iIndex);
System.out.println("Identifier = " +nameElement.getFirstChild().getNodeValue().trim());
}
}
nameNode = element.getElementsByTagName("dc:publisher");
for(int iIndex=0; iIndex< nameNode.getLength(); iIndex++)
{
if (nameNode.item(iIndex).getNodeType() ==Node.ELEMENT_NODE)
{
Element nameElement = (Element) nameNode.item(iIndex);
System.out.println("Publisher = " +nameElement.getFirstChild().getNodeValue().trim());
}
}
nameNode = element.getElementsByTagName("dc:subject");
for(int iIndex=0; iIndex< nameNode.getLength(); iIndex++)
{
if (nameNode.item(iIndex).getNodeType() ==Node.ELEMENT_NODE)
{
Element nameElement = (Element) nameNode.item(iIndex);
System.out.println("Subject = " +nameElement.getFirstChild().getNodeValue().trim());
}
}
nameNode = element.getElementsByTagName("dc:title");
for(int iIndex=0; iIndex< nameNode.getLength(); iIndex++)
{
if (nameNode.item(iIndex).getNodeType() ==Node.ELEMENT_NODE)
{
Element nameElement = (Element) nameNode.item(iIndex);
System.out.println("Title = " +nameElement.getFirstChild().getNodeValue().trim());
}
}
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
public static String getCharacterDataFromElement(Element e) {
Node child = e.getFirstChild();
if (child instanceof CharacterData) {
CharacterData cd = (CharacterData) child;
return cd.getData();
}
return "?";
}

}

No comments:

Post a Comment