Digg Widget for Blogger - VTD Extract XML

After I wrote the methodology to make an XSLT to extract the response from Digg, I got a request asking for a way to do this in VTD itself and return the response back to the calling module. So, before we dwell on using JQuery and creating a widget in Blogger for this, let us understand how to use VTD to process the response.

We have the following output returned from Digg

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="out.xsl"?>
<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"/>

Instead of using XSLT, we will use VTD to grab the result here. VTDNav object would be used to navigate the "story" node and "title" node and a HTML can be constructed out of this as we desire. I would strongly suggest you to check the following link, that explains how to use VTD to get the response XML from Digg first. The navigation part of the code 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();
//Declare an autopilot object of VTD
AutoPilot ap = new AutoPilot(vn);
ap.selectElement("story"); => select story node using Autopilot object.
String Title="";
String link="";
        link=    vn.toString(vn.getAttrVal("link"));    => Get the hyperlink into a variable "link"
        if (vn.toElement(VTDNav.FIRST_CHILD, "title"))
                  Title = vn.toString(vn.getText()); => Get the corresponding title tag into "Title" element
        System.out.println("<a href="+link+">"+Title+"</a>");    => The resulting HTML. We can push this into an array or use any approach for displaying in browser.          
 catch (NavException e1){
         System.out.println(" Exception during navigation "+e1);
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=3";

This would give the same output as we saw in the XSLT example earlier. Note that we need to feed the parameters to the GET request dynamically.We are now ready to pick up a JQuery example for the same and see how we can integrate this with Blogger.

No comments:

Post a Comment