Java Almanac Reminder

You know how you find cool resources on the web, and then space them? Then like a year later you find them again and wonder why you weren't using them all the time? I do this all the time. The last time I was smart enough to add a link somewhere obvious (next to my oft-used link to the JDK API) for the Java Almanac. This is sort of a public service announcement for Java programmers who may have not seen this site or have spaced it.

The site is fantastic. It's just a bunch of code examples for the Java API. Even when you *know* how something works, it's always nice to see the most basic example possible of it in action. Since the Almanac is constantly being updated, you can also find examples of newer API classes as well, which is also extremely handy. I was just thinking, "hey - I wonder if I should do this using Java 1.4's XML stuff... how does it work again?" and 3 clicks later I had a working example to look at:

The Quintessential Program to Create a DOM Document from an XML File

    import javax.xml.parsers.*;
    import org.w3c.dom.*;
    import org.xml.sax.*;

    public class BasicDom {
        public static void main(String[] args) {
            Document doc = parseXmlFile("infilename.xml", false);

        // Parses an XML file and returns a DOM document.
        // If validating is true, the contents is validated against the DTD
        // specified in the file.
        public static Document parseXmlFile(String filename, boolean validating) {
            try {
                // Create a builder factory
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

                // Create the builder and parse the file
                Document doc = factory.newDocumentBuilder().parse(new File(filename));
                return doc;
            } catch (SAXException e) {
                // A parsing error occurred; the xml input is not valid
            } catch (ParserConfigurationException e) {
            } catch (IOException e) {
            return null;

Tell me that isn't incredibly useful to have? Just click on the Browse button when you get to the site, find the package of the example you're looking for and choose one of the samples.

Insanely useful site, thanks to Patrick Chan for making it available (remember to buy his book!)... Now only if I could download it for offline use!


< Previous         Next >