Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.MessageDigest;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.events.XMLEvent;

public class Main {

    public static void main(String[] args) throws Exception {
        FileInputStream in1 = new FileInputStream(new File("file1.xml"));
        FileInputStream in2 = new FileInputStream(new File("file2.xml"));

        System.out.println(digest(in1));
        System.out.println(digest(in2));
    }

    public static String digest(InputStream in) throws Exception {
        MessageDigest messageDigest = null;
        XMLInputFactory inputFactory = XMLInputFactory.newFactory();
        messageDigest = MessageDigest.getInstance("MD5");
        XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
        while (eventReader.hasNext()) {
            XMLEvent event = eventReader.nextEvent();

            if (event.isStartElement()) {
                messageDigest.update(event.asStartElement().getName().toString().getBytes());
            } else if (event.isEndElement()) {
                messageDigest.update(event.asEndElement().getName().toString().getBytes());
            }
        }
        StringBuffer result = new StringBuffer();
        byte[] digest = messageDigest.digest();
        for (byte b : digest) {
            result.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));
        }
        return result.toString();
    }
}