8

I am parsing Xml file located in web server and storing parsed data in to database. for my app I am using data from database. I need to parse the xml file only if the file is modified otherwise no need to parse. So how can I know the file is modified? I know I can use "if-modified-since" header. But I need some examples of "if-modified-since" header
please help me.......

Giru Bhai
  • 14,370
  • 5
  • 46
  • 74
Prabhu M
  • 3,534
  • 8
  • 48
  • 87

2 Answers2

17

Since you are retrieving your .xml file from a web server, this should be relatively easy without having to do a server side MD5 sum.

If you are doing a HTTP request for the xml file you can simply perform a HEAD request from the web server and this will return if the file has changed/modified or if it doesn't exist. This is also lightweight and the best part is that the server should already do this for you.

Edit: re-reading your question, looks like you had the same idea. Here's the code.

import java.net.*;
import java.io.*;

// Using HTTP_NOT_MODIFIED
public static boolean Changed(String url){
    try {
      HttpURLConnection.setFollowRedirects(false);
      HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
      con.setRequestMethod("HEAD");
      return (con.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED);
    }
    catch (Exception e) {
       e.printStackTrace();
       return false;
    }
  }

// GET THE LAST MODIFIED TIME
public static long LastModified(String url)
{
  HttpURLConnection.setFollowRedirects(false);
  HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
  long date = con.getLastModified();

  if (date == 0)
    System.out.println("No last-modified information.");
  else
    System.out.println("Last-Modified: " + new Date(date));

  return date;
}

See:

Alternatively if your server supports them you can use ETags to find out if your file has been modified.

Justin Shield
  • 2,390
  • 16
  • 12
  • hi,can u explain the above method more clearly, that is "If the above method returns true means file is modified"? – Prabhu M Jul 14 '11 at 11:43
  • This does make you depend heavily on the "last modified" field, which is somethimes abused (* I think, correct me if I'm wrong *) to force no-caching and stuff. – Nanne Jul 14 '11 at 12:02
  • @Nanne - Ideally you should use the etags, after all this is what they're for. Though sometimes this isn't a viable option i.e. Load Balanced IIS Servers can't generate etags, or retreive etags from a CDN. Static files such as images or .xml files should be ok to use the last modified field. – Justin Shield Jul 14 '11 at 13:21
  • My point was, but I would be the first to admin that I don't have too much in-depth knowledge, that you should trust the server to provide correct data. If you can spare the clockcycles and traffic to just download it and md5 it you have more certainty I think (but at an obvious cost ofcourse). – Nanne Jul 14 '11 at 13:42
1

Calculate the MD5 of the file. You can save the old one and compare it?

If you don't know how, check out this for example: Getting a File's MD5 Checksum in Java

Community
  • 1
  • 1
Nanne
  • 64,065
  • 16
  • 119
  • 163