Digg Widget for Blogger - HTTP Request Using VTD

In the earlier post, we saw how to invoke a HTTP GET request using the traditional methods of Java. We also found that we need to write a lot of code for this and a shorter method could be much helpful for us. Since we have to parse the resulting XML to get the links, we have to use a XML parser for this. Across my blog, I have already covered DOM4J to some extent.

So, I decided to use VTD this time. VTD is the most powerful XML processor that is available free of cost in the industry today. Moreover, VTD offers some methods to directly invoke a HTTP request and grab the resulting XML as an object, so that the processing can be done readily!..cool..isn't. A VTD version of the same code, that invokes a HTTP get request to Digg is provided below

import com.ximpleware.*;
import java.io.*;
public class SampVTD
public static void sendGetRequest(String endpoint, String requestParameters)
java.lang.String urlStr = endpoint;
Boolean bool=false;
VTDGen vg = new VTDGen();
urlStr += "?" + requestParameters;
VTDNav vn = vg.getNav();
catch (IOException e)
public static void main(String args[])
    String url="http://services.digg.com/1.0/endpoint";
    String parameters="method=search.stories&query=peoplesoft&topic=programming&count=2";

Very straightforward. In a nutshell, we invoke the "parseHTTPURL" method of the VTDGen class, which accepts our HTTP URL and directly invokes a GET call. Further, the resulting XML is grabbed into a VTDNav object. To see, if we are getting the same output in the traditional method and in this approach, we invoke the "dumpXML" method of the VTDNav class. The HTTP response XML is written into out.xml in the same folder where SampVTD resides.

This method is relatively compact and also gives the resulting XML in a ready state for processing. (i.e. transforming into hyperlinks.) At the time of writing, the output from Digg for the GET request is provided below

<?xml version="1.0" encoding="UTF-8"?>
<stories count="2" offset="0" timestamp="1271499463" total="29" version="1.0">
    <story link="http://www.oracle.com/applications/peoplesoft/tools_tech/ent/ptools/application-development.html" submit_date="1269423261" diggs="1" id="20131750" comments="0" href="http://digg.com/programming/PeopleTools_Application_Development_Applications_Oracle" status="upcoming" media="news">
        <title>PeopleTools Application Development | Applications | Oracle</title>
        <user icon="" name="hari86av" profileviews="148" registered="1196363365"/>
        <topic name="Programming" short_name="programming"/>
        <container name="Technology" short_name="technology"/>
        <shorturl short_url="http://digg.com/d31MTBe" view_count="0"/>
    <story link="http://www.aired.in/2009/10/peoplesoft-admin-interview-questions.html" submit_date="1262492162" diggs="1" id="18226229" comments="0" href="http://digg.com/programming/PeopleSoft_Administration_Interview_Questions_2" status="upcoming" media="news">
        <description>Help prepare for an Peoplesoft Administrator Interview</description>
        <title>PeopleSoft Administration Interview Questions</title>
        <user icon="" name="datalink4i" profileviews="307" registered="1160585515"/>
        <topic name="Programming" short_name="programming"/>
        <container name="Technology" short_name="technology"/>
        <shorturl short_url="http://digg.com/d31ETTR" view_count="25"/>

So, the XML is ready with us. We can now apply a XSLT transformation to it to convert it in the format we expect. This step is explained in the next post.

No comments:

Post a Comment