Java XML Document from File loadXmlFromFile(String xmlFileName)

Here you can find the source of loadXmlFromFile(String xmlFileName)

Description

Loads an xml file on disk into a document

License

Open Source License

Parameter

Parameter Description
xmlFileName - name of the file on disk, including path

Return

Document - loaded from the specified xml file on disk

Declaration

public static Document loadXmlFromFile(String xmlFileName) 

Method Source Code


//package com.java2s;
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 {
    /**//  w w  w.  j a v a  2 s. c  om
     * Loads an xml file on disk into a document
     *
     * @param  xmlFileName - name of the file on disk, including path
     * @return Document    - loaded from the specified xml file on disk
     */
    public static Document loadXmlFromFile(String xmlFileName) {
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        docFactory.setNamespaceAware(true);

        try {
            return docFactory.newDocumentBuilder().parse(new File(xmlFileName));
        } catch (ParserConfigurationException e) {
            throw new RuntimeException("Error setting up UI xml page model parser -- " + e.getMessage());
        } catch (SAXException e) {
            throw new RuntimeException(
                    "Error parsing UI xml page model file [" + xmlFileName + "]  -- " + e.getMessage());
        } catch (IOException e) {
            throw new RuntimeException(
                    "Error opening or reading UI xml page model file [" + xmlFileName + "]  -- " + e.getMessage());
        }
    }
}

Related

  1. loadXml(String fileName)
  2. loadXml(String filePath)
  3. loadXml(String fn)
  4. loadXMLDocument(String path)
  5. loadXmlDocumentFromFile(@Nonnull File file)
  6. read(InputStream input)
  7. read(InputStream xml)
  8. read(java.io.File file)
  9. read(String file)