Java tutorial
//package com.java2s; /** * XMLUtils.java * ---------------------------------------------------------------------------------- * * Copyright (C) 2008 www.integratedmodelling.org * Created: Jan 21, 2008 * * ---------------------------------------------------------------------------------- * This file is part of ThinklabPersistencePlugin. * * ThinklabPersistencePlugin is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * ThinklabPersistencePlugin is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with the software; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * * ---------------------------------------------------------------------------------- * * @copyright 2008 www.integratedmodelling.org * @author Ioannis N. Athanasiadis (ioannis@athanasiadis.info) * @date Jan 21, 2008 * @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 * @link http://www.integratedmodelling.org **/ import java.io.File; import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.xml.sax.SAXException; public class Main { /** * This method parses an XML file and returns an {@code org.w3c.dom.Document} * @param f * @return */ public static Document parseXMLFile(File f) { Document dom = null; // get the factory DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try { //Using factory get an instance of document builder DocumentBuilder db = dbf.newDocumentBuilder(); //parse using builder to get DOM representation of the XML file dom = db.parse(f); } catch (ParserConfigurationException pce) { pce.printStackTrace(); } catch (SAXException se) { se.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } return dom; } }