Java XML Document from File readXmlFile(String fileName)

Here you can find the source of readXmlFile(String fileName)

Description

parse xmlFile in a Document

License

Apache License

Parameter

Parameter Description
fileName full name of a xml file

Exception

Parameter Description
IOException an exception

Return

a Dom Document

Declaration

public static Document readXmlFile(String fileName) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.File;

import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

public class Main {
    /**/*from  w w w.j a  va 2 s  . c  o m*/
     * parse xmlFile in a Document
     * 
     * @param fileName
     *            full name of a xml file
     * @return a Dom Document
     * @throws IOException
     */
    public static Document readXmlFile(String fileName) throws IOException {

        Document doc = null;

        try {
            File xmlFile = new File(fileName);
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = factory.newDocumentBuilder();
            doc = docBuilder.parse(xmlFile);
        } catch (Exception e) {
            System.out.println("Error on parsing XML file : " + fileName);
            e.printStackTrace();
            throw new IOException("Error on parsing XML file : " + fileName);
        }

        return doc;
    }
}

Related

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