The Java Program: BasicDom.java

  1 import java.io.*;
  2 import javax.xml.parsers.*;
  3 import org.w3c.dom.*;
  4 import org.xml.sax.*;
  5     
  6 public class BasicDom {
  7     public static void main (String[] args) {
  8         final Document doc = parseXmlFile(args[0], false);
  9         System.out.println (doc);
 10     }
 11     
 12     // Parses an XML file and returns a DOM document.
 13     // If validating is true, the contents is validated against the DTD
 14     // specified in the file.
 15     public static Document parseXmlFile(String filename, boolean validating) {
 16         try {
 17             // Create a builder factory
 18             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
 19             factory.setValidating(validating);
 20     
 21             // Create the builder and parse the file
 22             Document doc = factory.newDocumentBuilder().parse(new File(filename));
 23             return doc;
 24         } catch (SAXException e) {
 25             // A parsing error occurred; the xml input is not valid
 26         } catch (ParserConfigurationException e) {
 27         } catch (IOException e) {
 28         }
 29         return null;
 30     }
 31 }