Java tutorial
//package com.java2s; /* * Copyright (c) 2010-2012 Research In Motion Limited. All rights reserved. * * This program and the accompanying materials are made available * under the terms of the Apache License, Version 2.0, * which accompanies this distribution and is available at * * http://www.apache.org/licenses/LICENSE-2.0 * */ import java.io.File; import java.io.IOException; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.xml.sax.SAXException; public class Main { public static Document openXmlFile(String filename, boolean validating) { return openXmlFile(new File(filename), validating); } public static Document openXmlFile(File file, boolean validating) { try { // Create a builder factory DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(validating); // Create the builder and parse the file Document doc = factory.newDocumentBuilder().parse(file); return doc; } catch (SAXException e) { // A parsing error occurred; the xml input is not valid } catch (ParserConfigurationException ex) { System.out.println(ex.getMessage()); } catch (IOException ex) { System.out.println(ex.getMessage()); } return null; } }