Java XML Document from File readXMLFile(String filename)

Here you can find the source of readXMLFile(String filename)

Description

Reads the xml file and returns the Document of it.

License

Open Source License

Parameter

Parameter Description

Return

org.w3c.dom.Document of the xml file

Declaration

public static Document readXMLFile(String filename) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * QBiC Offer Generator provides an infrastructure for creating offers using QBiC portal and
 * infrastructure. Copyright (C) 2018 Benjamin Sailer
 *
 * This program 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.
 *
 * This program 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 this program. If
 * not, see http://www.gnu.org/licenses/.
 *******************************************************************************/

import org.w3c.dom.Document;

import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import java.io.File;
import java.io.IOException;

public class Main {
    /**/*  w w w  .ja  v a  2  s.  c  om*/
     * Reads the xml file and returns the Document of it.
     * @param filename: file to parse
     * @return org.w3c.dom.Document of the xml file
     */
    public static Document readXMLFile(String filename) {

        // setup the document builder
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = null;
        try {
            dBuilder = dbFactory.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        }
        assert dBuilder != null;

        // parse the xml file
        File XMLFile = new File(filename);
        Document doc = null;
        try {
            doc = dBuilder.parse(XMLFile);
        } catch (SAXException | IOException e) {
            e.printStackTrace();
        }
        assert doc != null;

        return doc;
    }
}

Related

  1. readXML(String path)
  2. readXMLFile(File file)
  3. readXMLFile(String filename)
  4. readXmlFile(String fileName)
  5. readXmlFile(String fileName)
  6. ReadXMLFile1(String in_FileName, String in_TagName)
  7. ReadXMLFile3(String in_FileName, String in_TagName)