Παράδειγμα: υπολογισμός μέσου όρου

import javax.xml.parsers.*;
import java.io.*;
import org.w3c.dom.*;

class Average {
    public static void main(String args[]) {

        if (args.length != 2) {
            System.err.println("Usage: Average element file");
            System.exit(1);
        }

        Document doc = null;
        try {
            // Create the DocumentBuilderFactory
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            // Create the document builder
            DocumentBuilder db = dbf.newDocumentBuilder();
            // Create DOM document from the file
            doc = db.parse(new File(args[1]));
        } catch (Exception e) {
            System.err.println("Parsing failed: " + e);
            System.exit(1);
        }

        NodeList nodes = doc.getElementsByTagName(args[0]);
        double sum = 0.0;
        for (int i = 0; i < nodes.getLength(); i++) {
            String grade = nodes.item(i).getFirstChild().getNodeValue();
            sum += (Integer.valueOf(grade)).doubleValue();
        }
        System.out.println(sum / nodes.getLength());
    }
}